- package com.billows.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- * @author Billows.Van haidii@126.com
- * @version 创建时间:2008-4-30 上午10:53:09 类说明
- */
- public class JdbcUtil {
- static {
- try {
- EnvironmentConfig ec=EnvironmentConfig.getInstance();
- String driverClassName=ec.getPropertyValue("/config/jdbc.properties", "jdbc.driverClassName");
- Class.forName(driverClassName);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static Connection getConnection() throws SQLException {
- Connection conn = null;
- EnvironmentConfig ec=EnvironmentConfig.getInstance();
- String url=ec.getPropertyValue("/config/jdbc.properties", "jdbc.url");
- String name=ec.getPropertyValue("/config/jdbc.properties", "jdbc.username");
- String pwd=ec.getPropertyValue("/config/jdbc.properties", "jdbc.password");
- conn = DriverManager.getConnection(url, name, pwd);
- return conn;
- }
- public static void release(Connection conn, PreparedStatement ps,ResultSet rs) {
- if (rs != null)try {rs.close();} catch (SQLException e) {e.printStackTrace();}
- if (ps != null) try {ps.close();} catch (SQLException e) {e.printStackTrace();}
- if (conn!= null)try {conn.close();} catch (SQLException e) {e.printStackTrace();}
- }
- public static void release(Object obj) {
- if (obj instanceof Connection) {
- Connection conn = (Connection) obj;
- if (conn != null)
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (obj instanceof PreparedStatement) {
- PreparedStatement ps = (PreparedStatement) obj;
- if (ps != null)
- try {
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- if (obj instanceof ResultSet) {
- ResultSet rs = (ResultSet) obj;
- if (rs != null)
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
JdbcUtil
最新推荐文章于 2020-11-20 00:22:22 发布