快速业务通道

Hibernate3.x调用存储过程大揭密 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-18
???ààμ??úè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ìμ?μ÷ó??£

??£?×ü?á

±?à

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号