日期:2014-05-16  浏览次数:20503 次

JDBC公共操作方法(二):获取数据库连接 Connection

1.?获取数据库连接

?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public final class JDBCUtil {

	private static final Logger LOG = Logger.getLogger(JDBCUtil.class);

	//使用ThreadLocal保存Connection
	private static ThreadLocal<Connection> local = new ThreadLocal<Connection>();

	private static JDBCUtil instance = new JDBCUtil();

	private JDBCUtil() {
	}

	public static JDBCUtil getInstance() {
		return instance;
	}

	/**
	 * @Title: getConnection
	 * @Description: 获取数据库连接
	 * @return Connection
	 * @author
	 * @date 2011-12-29
	 */
	public static Connection getConnection() {

		// 获取jdbc.properties中配置的key-value
		ParseSqlPropertiesFile.getInstance().getJDBCConfig();

		String driverClass = JDBCConstants.jdbcMap.get("jdbc.driverclass");
		String url = JDBCConstants.jdbcMap.get("jdbc.url");
		String user = JDBCConstants.jdbcMap.get("jdbc.username");
		String password = JDBCConstants.jdbcMap.get("jdbc.password");

		// 记录连接属性日志
		LogJDBCProperties.getInstance().logConnectionProperty(driverClass, url,
				user, password);

		Connection connection = null;

		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			LOG.error("can not find class:'" + driverClass + "'", e);
			return connection;
		} catch (Exception e) {
			LOG.error("load driverClass faile!", e);
			return connection;
		}

		try {
			connection = DriverManager.getConnection(url, user, password);
			
			if (null == local.get()) {
				local.set(connection);
			}
		} catch (SQLException e) {
			LOG.error("get connection faile!", e);
		} catch (Exception e) {
			LOG.error("get connection faile!", e);
		}
		return local.get();
	}

	/**
	 * @Title: close
	 * @Description: 关闭与数据库的连接
	 * @param conn
	 * @param rs
	 * @param s
	 * @author
	 * @date 2011-12-29
	 */
	public static void close(Connection conn, ResultSet rs, Statement s) {

		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				LOG.error("close ResultSet faile!", e);
			}
		}
		if (null != s) {
			try {
				s.close();
			} catch (SQLException e) {
				LOG.error("close Statement faile!", e);
			}
		}
		if (null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				LOG.error("close Connection faile!", e);
			}
		}
	}

	public static void rollback(Connection connection) {

		if (null != connection) {
			try {
				connection.rollback();
			} catch (SQLException e) {
				LOG.error("connection rollback faile!", e);
			}
		}
	}

	/*
	 * public static void main(String[] args) {
	 * 
	 * Connection connection = JDBCUtil.getInstance().getConnection();
	 * 
	 * if (null != connection) { boolean isClose = true; try { isClose =
	 * connection.isClosed(); } catch (SQLException e) { e.printStackTrace(); }
	 * if (isClose) { System.out.println("can not connect to database."); } else
	 * { System.out.println("connect to database success."); } } else {
	 * System.out .println("connection is null,can not connect to database."); }
	 * close(connection,null,null); }
	 */
}
?