您的位置:首页 > 编程语言 > Java开发

java 从数据源获取连接工具类DBUtil 实例

2013-08-27 21:35 357 查看
package com.asiainfo.common.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import com.asiainfo.common.Constants;
import com.asiainfo.common.exception.DAOException;
import com.asiainfo.common.service.ServiceLocator;
import com.asiainfo.common.service.ServiceLocatorException;

/**
 * 从数据源获取数据库连接的工具类,并提供相关获取集合,修改以及简单事务控制
 * 
 * @author reborn
 * @see
 */
public class DBConnection
{
private static Logger logger = Logger.getLogger(DBConnection.class);

public static Connection getConnection() throws DAOException
{
return getConnection(Constants.DS_ZCRM);
}

public static Connection getConnection(String dsname) throws DAOException
{
Connection con = null;
try {
DataSource ds = (DataSource) ServiceLocator.getBean(dsname);
con = ds.getConnection();
if (con == null || con.isClosed()) {
logger.error("连接使用已经超时,该连接已经被回收!");
}
logger.debug("获得的当前线程【" + Thread.currentThread().getId() + "】上的连接为:" + con.hashCode());
}
catch (ServiceLocatorException e) {
logger.fatal(e);
throw new DAOException("", "", e);
}
catch (SQLException e) {
// 需要写日志
logger.fatal("连接使用已经超时,该连接已经被回收:" + e);
throw new DAOException("", "", e);
}
return con;
}

// 关闭预制sql语句的连接
public static void close(Connection con, PreparedStatement stmt, ResultSet rs)
{
//
try {
if (rs != null)
rs.close();
}
catch (SQLException se) {
logger.error("Close ResultSet Error : " + se);
}

//
try {
if (stmt != null)
stmt.close();
}
catch (SQLException se) {
logger.error("Close PreparedStatement Error : " + se);
}

//
try {
if (con != null && !con.isClosed()) {
logger.debug("关闭当前线程【" + Thread.currentThread().getId() + "】上的连接为:" + con.hashCode());
con.close();
}
}
catch (SQLException e) {
// 需要写日志
logger.error("数据库关闭异常:" + e);
}
finally {
con = null;
}
}

public static void close(Connection con, Statement stmt, ResultSet rs)
{
//
try {
if (rs != null)
rs.close();
}
catch (SQLException se) {
logger.error("Close ResultSet Error : " + se);
}

//
try {
if (stmt != null)
stmt.close();
}
catch (SQLException se) {
logger.error("Close PreparedStatement Error : " + se);
}

//
try {
if (con != null && !con.isClosed()) {
logger.debug("关闭当前线程【" + Thread.currentThread().getId() + "】上的连接为:" + con.hashCode());
con.close();
}
}
catch (SQLException e) {
// 需要写日志
logger.error("数据库关闭异常:" + e);
}
finally {
con = null;
}
}

public static void close(PreparedStatement stmt, ResultSet rs)
{
//
try {
if (rs != null)
rs.close();
}
catch (SQLException se) {
logger.error("Close ResultSet Error : " + se);
}

//
try {
if (stmt != null)
stmt.close();
}
catch (SQLException se) {
logger.error("Close PreparedStatement Error : " + se);
}
}

public static void commit(Connection connection) throws SQLException
{
if (connection != null) {
connection.commit();
logger.debug("提交当前线程【" + Thread.currentThread().getId() + "】上的连接为:" + connection.hashCode());
}
}

public static void rollback(Connection connection)
{
try {
if (connection != null) {
logger.debug("回滚当前线程【" + Thread.currentThread().getId() + "】上的连接为:" + connection.hashCode());
connection.rollback();
}
}
catch (SQLException e) {
logger.error("rollback Error : " + e);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: