zoukankan      html  css  js  c++  java
  • 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 
  • 相关阅读:
    存储过程
    .Net经典面试题
    《锋利的Jquery》
    WPF-1
    ios-5-类别和协议
    ios-4-创建单例模式
    ios-3-简单内存管理
    ios-2
    ios -1
    <<ASP.NET MVC4 Web编程>>笔记
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1829070.html
Copyright © 2011-2022 走看看