国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用ThreadLocal簡化struts開發

2019-11-17 06:18:58
字體:
來源:轉載
供稿:網友

/* 獲取connection 對象* /
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import Oracle.jdbc.driver.OracleDriver;

/**
 * @author sfluo
 *
 * TODO To change the template for this generated type comment go to Window -
 * PReferences - Java - Code Style - Code Templates
 */
public class DBUtil {

 public static Connection conn;

 public static Connection getConnection() {

  try {

   DriverManager.registerDriver(new OracleDriver());
   conn = DriverManager.getConnection(
     "jdbc:oracle:thin:@10.100.143.161:1521:gb02", "gib", "gib");
  } catch (SQLException e) {
   e.printStackTrace();
  }

  return conn;
 }

}

利用ThreadLocal保存connection對象
package conn;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author sfluo
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ConnectionManage {
 private static ThreadLocal currentConn = new ThreadLocal();

 public static Connection currentConnection() {

  Connection conn = (Connection) currentConn.get();
  if (conn == null) {
   conn = DBUtil.getConnection();
   currentConn.set(conn);
   openTransaction();
  }
  return conn;
 }

 public static void closeConnection() {
  try {
   Connection conn = (Connection) currentConn.get();
   currentConn.set(null);
   if (conn != null) {
    conn.close();
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 public static void openTransaction() {
  try {
   Connection conn = currentConnection();
   conn.setAutoCommit(false);
   conn.setTransactionIsolation(2);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

 public static void commit() {
  try {
   Connection conn = currentConnection();
   if (conn != null)
    conn.commit();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平顺县| 安吉县| 新宁县| 湟源县| 民县| 陵川县| 弥渡县| 灵武市| 广南县| 宽甸| 八宿县| 鄢陵县| 木兰县| 蓝山县| 吉安县| 临海市| 门头沟区| 萨迦县| 石屏县| 海宁市| 青海省| 鸡东县| 二连浩特市| 柳河县| 濮阳市| 宁德市| 德安县| 教育| 巴彦淖尔市| 海晏县| 孟津县| 潼关县| 宜良县| 永城市| 类乌齐县| 辽源市| 东兴市| 永善县| 武功县| 柞水县| 宣威市|