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 
  • 相关阅读:
    C面试复习笔记
    Java面试复习笔记
    Jdk1.6 HTTPS访问问题解决办法
    百度地图轨迹回放,自定义路书,边走边画线
    简单的代码生成小工具(支持模板)
    card布局解决复杂操作的布局问题
    tabpanel如何隐藏页签表头以及基本用法总结
    ExtJS4.2下将表单元素放在菜单时不能进行拷贝的问题解决办法
    照片元数据信息以及在照片中写入gps信息
    带名称空间的xml数据查询
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1829070.html
Copyright © 2011-2022 走看看