📄 jdbcutil.java
字号:
/*
* @(#)JdbcUtils.java Aug 2, 2007
* Copyright 2007 ThinkJ organization, Inc. All rights reserved
*/
package com.qrsx.shop.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*@Author:李世海
*@Address:青岛软件园
*@Date: Mar 26, 2009
*/
@SuppressWarnings("unchecked")
public class JdbcUtil {
private static ThreadLocal threadConnection = new ThreadLocal();
//private static ThreadLocal threadTransaction = new ThreadLocal();
/**
* 连接MsSql数据库
* @return
*/
public static Connection getMsConnection(){
Connection conn = null;
String url = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=login;User=sa;Password=sa";
conn = DriverManager.getConnection(url);
if( conn==null ){
throw new SQLException("Can't connect MsSql!");
}
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}catch(SQLException e){
System.out.println("url:"+url);
e.printStackTrace();
}
return conn;
}
/**
* 连接MsSql数据库
* @return
*/
public static Connection getMySqlConnection(){
Connection conn = null;
String url = null;
try{
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://localhost:3306/shop";
conn = DriverManager.getConnection(url,"root","");
if( conn==null ){
throw new SQLException("Can't connect MsSql!");
}
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}catch(SQLException e){
System.out.println("url:"+url);
e.printStackTrace();
}
return conn;
}
/**
* 获取当前Connection
* @return
* @throws SQLException
*/
public static Connection getCurrentConnection() throws SQLException{
Connection conn = (Connection) threadConnection.get();
if (conn == null) {
System.out.println("Opening new conncetion for this threadConnection.");
conn = getMySqlConnection();
threadConnection.set(conn);
}
return conn;
}
/**
* 获取当前Connection
* @return
* @throws SQLException
*/
public static Connection getCurrentConnection( boolean isTransaction ) throws SQLException{
Connection conn = getCurrentConnection();
//启动事物
if( isTransaction ){
conn.setAutoCommit(false);
}
return conn;
}
/**
* 关闭当前Connection,并提交事物
* @throws SQLException
*/
public static void closeCurrentConnection(){
try{
Connection conn = (Connection) threadConnection.get();
threadConnection.set(null);
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
/**
* 关闭指定的数据库链接
* @param conn
* @throws SQLException
*/
public static void closeConnection( Connection conn ) throws SQLException{
try{
if( conn!=null ){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
/*******************************************************
* Transaction操作方法
*******************************************************/
/**
* 开始一个新事务
*/
public static void beginTransaction(){
try{
getCurrentConnection().setAutoCommit(false);
}catch(SQLException e){
e.printStackTrace();
}
}
/**
* 提交数据库的事务
* @throws SQLException
*/
public static void commitTransaction(){
try{
getCurrentConnection().commit();
}catch(SQLException e){
e.printStackTrace();
}
}
/**
* 回滚当前事务方法的实现
* @throws SQLException
*/
public static void rollbackTransaction(){
try{
getCurrentConnection().rollback();
}catch(SQLException e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -