Hibernate<Session>与Jdbc<Connection>

 1  package  com.klein;
 2 
 3  import  java.sql.Connection;
 4 
 5  import  java.sql.ResultSet;
 6  import  java.sql.Statement;
 7 
 8  import  org.hibernate.Session;
 9  import  org.hibernate.SessionFactory;
10  import  org.hibernate.cfg.Configuration;
11 
12  /**
13   * This class show how to use the jdbc Connection interface via Hibernate
14   * Session interface. Please prepare the configuration by yourself.
15   * 
16   *  @author  Klein
17   * 
18    */
19  public   class  ConnectionTest {
20 
21       /**
22       *  @param  args
23        */
24       public   static   void  main(String[] args) {
25          Configuration configuration  =   new  Configuration().configure();
26          SessionFactory factory  =  configuration.buildSessionFactory();
27          Session session  =  factory.getCurrentSession();
28 
29           //  Make sure the Transaction is active before using the connection.
30          session.beginTransaction();
31 
32          Connection conn  =  session.connection();
33           try  {
34              conn.setAutoCommit( false );
35              Statement stmt  =  conn.createStatement();
36              String sql  =   " select sysdate from dual " ;
37              ResultSet rs  =  stmt.executeQuery(sql);
38               while  (rs.next()) {
39                  System.out.println(rs.getString( 1 ));
40              }
41              conn.commit();
42 
43          }  catch  (Exception e) {
44               try  {
45                  e.printStackTrace();
46                  conn.rollback();
47                  conn.close();
48              }  catch  (Exception e2) {
49                  e2.printStackTrace();
50              }
51          }  finally  {
52               try  {
53                  conn.close();
54 
55              }  catch  (Exception e2) {
56                  e2.printStackTrace();
57              }
58          }
59      }
60  }
61 

 

转载于:https://www.cnblogs.com/kelin1314/archive/2010/09/17/1829070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值