• 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 
  • 相关阅读:
    小程序用户拒绝授权地理位置的处理办法
    云开发小程序数据库权限有限,通过云函数修改数据库评论信息
    小程序仿照微信朋友圈点击评论键盘输入
    小程序wx.previewImage查看图片再次点击返回时重新加载页面问题
    js手机端判断滑动还是点击
    Proxy
    Reflect.has检测对象是否拥有某个属性
    简单的axios请求返回数据解构赋值
    为windows terminal 配置 conda
    git clone 遇到问题:fatal: unable to access 'https://github.comxxxxxxxxxxx':
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1829070.html
Copyright © 2020-2023  润新知