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);
}
}
}
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);
}
}
}
相关文章推荐
- java获取url连接地址参数的工具类
- JavaWeb之JDBC(二)采用读取配置文件方式编写JDBC的工具类,获取数据库的连接
- Java使用JDBC连接数据库完整的工具类实例
- JDBC实例--通过连接工具类DBUtil +配置文件来获取连接数据库,方便又快捷
- java使用JNDI 获取weblogic配置的数据源 连接数据库
- java无数据源连接Access数据库实例
- DataSource数据源连接数据库的配置及实例(Java)
- JDBC 实例--JDBC通过工具类DBUtil连接到数据库,让我们不再恐惧操作数据库
- java使用JNDI 获取weblogic配置的数据源 连接数据库
- java使用JNDI 获取weblogic配置的数据源 连接数据库
- Java日期操作方法工具类实例【包含日期比较大小,相加减,判断,验证,获取年份等】
- 用JAVA代码获取Weblogic配置的JNDI 数据源连接
- 用JAVA代码获取Weblogic配置的JNDI 数据源连接
- 关于数据库连接操作的工具类DBUtil.java
- 用JAVA代码获取Weblogic配置的JNDI 数据源连接
- java 获取数组(二维数组)长度实例程序
- java连接postgreSQL数据库实例(转)
- Java连接各种数据库的实例
- Java连接各种数据库的实例
- java连接Redis的工具类