???ààμ??úèYè???£o
// hibernateμ÷ó?′?′¢1y3ì
public class ProcTest ...{
public static void main(String[] args) throws Exception ...{
?? ProcTest proc = new ProcTest();
?? Session session = HibernateSessionFactory.getSession();
?? proc.testProcQuery(session);
?? proc.testProcUpdate(session);
?? System.out.println("update successfully");
?? proc.testProcInsert(session);
?? System.out.println("insert successfully");
?? proc.testProcDelete(session);
?? System.out.println("delete successfully");
?? session.close();
}
// 2aê?êμ??2é?ˉμ?′?′¢1y3ì
private void testProcQuery(Session session) throws Exception ...{
?? //2é?ˉó??§áD±í
???????? List list = session.getNamedQuery("getUserList").list();
???????? for (int i = 0; i < list.size(); i++) ...{
???????????? User user = (User) list.get(i);
???????????? System.out.print("Dòo?: " + (i+1));
???????????? System.out.print(", userid: " + user.getUserid());
???????????? System.out.print(", name: " + user.getName());
???????????? System.out.println(", blog: " + user.getBlog());
???????? }
}
/**//**
?? * 2aê?êμ???üD?μ?′?′¢1y3ì
?? * @throws Exception
?? */
private void testProcUpdate(Session session) throws Exception ...{
?? //?üD?ó??§D??¢
?? Transaction tx = session.beginTransaction();
???????? Connection con = session.connection();
???????? String procedure = "{call updateUser(?, ?, ?)}";
???????? CallableStatement cstmt = con.prepareCall(procedure);
???????? cstmt.setString(1, "3?xx");
???????? cstmt.setString(2, "http://www.blogjava.net/sterningChen");
???????? cstmt.setString(3, "sterning");
???????? cstmt.executeUpdate();
???????? tx.commit();
}
// 2aê?êμ??2?è?μ?′?′¢1y3ì
private void testProcInsert(Session session) throws Exception ...{
?? //′′?¨ó??§D??¢
?? session.beginTransaction();
???????? PreparedStatement st = session.connection().prepareStatement("{call
createUser(?, ?, ?)}");
???????? st.setString(1, "amigo");
???????? st.setString(2, "°¢??1?");
???????? st.setString(3, "http://www.wblogjava.net/amigoxie");
???????? st.execute();
???????? session.getTransaction().commit();
}
// 2aê?êμ??é?3yμ?′?′¢1y3ì
private void testProcDelete(Session session) throws Exception ...{
?? //é?3yó??§D??¢
?? session.beginTransaction();
?? PreparedStatement st = session.connection().prepareStatement("{call deleteUser
(?)}");
???????? st.setString(1, "amigo");
???????? st.execute();
???????? session.getTransaction().commit();
}
}
?ú±?àà?D£?μ÷ó?2é?ˉàà′?′¢1y3ìê±£?μ÷ó?session.getNamedQuery("…")·?·¨à′??μ?User.hbm.xml?D????μ?2é?ˉ′?′¢1y3ì?£?ú??óàμ?′?′¢1y3ìμ÷ó?μ?2aê??D£?ê×?èí¨1yhibernateμ?session??μ?connection£?è?oóμ÷ó?connection???óμ??àó|·?·¨à′êμ??′?′¢1y3ìμ?μ÷ó??£
??£?×ü?á
±?à |