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 
  • 相关阅读:
    apache和tomcat有什么不同,为什么要整合apache 和tomcat?
    servlet
    关于Spring配置文件xml文档的schema约束
    request对象和response对象
    多线程
    数据结构得到连续数据的手段java Enumeration
    程序员八荣八耻
    windows更改MySQL存储路径
    Tomcat源码学习(1)
    Tomcat源码学习(2)——启动过程分析
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1829070.html
Copyright © 2011-2022 走看看