1 package com.xx.questionnaire.util.dao; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 import java.sql.Statement; 9 import java.util.Properties; 10 11 import javax.sql.DataSource; 12 13 import org.apache.commons.dbcp2.BasicDataSourceFactory; 14 import org.apache.log4j.Logger; 15 16 /** 17 * @version 0.1 18 * @author Administrator 19 * 20 */ 21 public class Dbcp_connection implements JdbcI { 22 static Logger log = Logger.getLogger(Dbcp_connection.class); 23 private Connection connection; 24 /** 25 * 在静态块中初始化获得DBCP数据源连接池 26 */ 27 static { 28 getDataSource(); 29 log.info("数据源已在静态块中初始化完成。"); 30 } 31 32 /** 33 *@return 返回Connection连接 34 */ 35 public Connection getConnection() { 36 if (connection == null || connection.equals("")) { 37 try { 38 setConnection(getDataSource().getConnection()); 39 log.info("从数据源中获取连接!"); 40 return connection; 41 } catch (SQLException e) { 42 log.error("对不起,数据库连接获取失败!"); 43 e.printStackTrace(); 44 } 45 } 46 return null; 47 } 48 /** 49 * @param sql SQL语句 50 * @return PrepaedStatement对象 51 */ 52 public PreparedStatement getPStatement(String sql) { 53 if (sql == null || sql.equals("")) { 54 log.error("错误,SQL语句不允许为空!"); 55 return null; 56 } 57 PreparedStatement ps = null; 58 try { 59 ps = getConnection().prepareStatement(sql); 60 log.info("SQL语句" + sql + "已经过预编译!"); 61 return ps; 62 } catch (SQLException e) { 63 log.error("异常,SQL语句" + sql + "预编译失败!"); 64 e.printStackTrace(); 65 } 66 67 return null; 68 } 69 70 /** 71 * 强制释放Connection连接。 72 * 73 */ 74 public void close() { 75 try { 76 this.connection.close(); 77 log.info("Connection object" + this.connection.hashCode() 78 + "被强制释放!"); 79 } catch (SQLException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 84 } 85 86 87 public void close(Statement ps) { 88 // TODO Auto-generated method stub 89 try { 90 91 ps.close(); 92 this.connection.close(); 93 log.info("Statement(" 94 + ps.hashCode() + "),Connection(" 95 + this.connection.hashCode() + ")已被释放!"); 96 } catch (SQLException e) { 97 log.error("异常,JDBC对象关闭失败!"); 98 e.printStackTrace(); 99 } 100 101 } 102 /** 103 * 按照顺序释放JDBC连接。 104 */ 105 public void close(Statement ps, ResultSet rs) { 106 // TODO Auto-generated method stub 107 try { 108 rs.close(); 109 ps.close(); 110 this.connection.close(); 111 log.info("ResultSet(" + rs.hashCode() + "),Statement(" 112 + ps.hashCode() + "),Connection(" 113 + this.connection.hashCode() + ")已被释放!"); 114 } catch (SQLException e) { 115 log.error("异常,JDBC对象关闭失败!"); 116 e.printStackTrace(); 117 } 118 119 } 120 121 public static void main(String[] args) { 122 // TODO Auto-generated method stub 123 /* 1 */ 124 DataSource dds = getDataSource(); 125 System.out.println(dds == null ? "数据源失败" : "数据源成功"); 126 127 } 128 129 /** 130 * 全局公用的数据源 131 * 132 * @return DataSource Object 133 */ 134 private static DataSource getDataSource() { 135 String dbcpUrl = "dbcp-mysql.properties"; 136 137 DataSource ds; 138 Properties p = new Properties(); 139 try { 140 // 通过类的类加载器将属性文件加载至输入流 141 p.load(JDBCUtil.class.getClassLoader().getResourceAsStream(dbcpUrl)); 142 log.info("DBCP配置文件已加载!"); 143 144 ds = BasicDataSourceFactory.createDataSource(p); 145 log.info("数据源获取成功!"); 146 return ds; 147 148 } catch (IOException e) { 149 log.error("异常,可能是配置文件读取失败。"); 150 e.printStackTrace(); 151 } catch (Exception e) { 152 e.printStackTrace(); 153 } 154 155 return null; 156 } 157 158 private void setConnection(Connection connection) { 159 log.info("获取连接,Hash地址为:" + connection.hashCode()); 160 this.connection = connection; 161 } 162 163 public DataSource getDS() { 164 // TODO Auto-generated method stub 165 log.info("数据源被外部调用!"); 166 return getDataSource(); 167 } 168 169 }
时间: 2024-10-08 07:35:45