package com.pc.jdbc.mydatasource;import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;import javax.sql.DataSource;/*** 自定义连接池** @author Switch* @data 2016年9月30日* @version V1.0*/
public class MyDataSource implements DataSource {/*** 队列模拟线程池*/private static LinkedList<Connection> dataSource = null;/*** 初始化连接池*/static {dataSource = new LinkedList<>();for (int i = 0; i < 20; i++) {// 通过工具类获取原始连接对象Connection ct = JDBCUtils.getConnection();// 将连接对象进行装饰,实现close的方法的覆盖,归还到连接池MyConnection myCt = new MyConnection(ct, dataSource);dataSource.add(myCt);}}/*** 从线程池中获取连接*/@Overridepublic Connection getConnection() throws SQLException {// 该版本并未考虑多线程及空闲连接和激活连接// 所以说当连接不够时,只会再添加一些连接if (dataSource.isEmpty()) {for (int i = 0; i < 5; i++) {dataSource.add(new MyConnection(JDBCUtils.getConnection(), dataSource));}}return dataSource.removeFirst();}@Overridepublic PrintWriter getLogWriter() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic int getLoginTimeout() throws SQLException {// TODO Auto-generated method stubreturn 0;}@Overridepublic Logger getParentLogger() throws SQLFeatureNotSupportedException {// TODO Auto-generated method stubreturn null;}@Overridepublic void setLogWriter(PrintWriter arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setLoginTimeout(int arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic boolean isWrapperFor(Class<?> arg0) throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic <T> T unwrap(Class<T> arg0) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Connection getConnection(String arg0, String arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}}
package com.pc.jdbc.mydatasource;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;/*** JDBC工具类* @author Switch* @data 2016年9月30日* @version V1.0*/
public class JDBCUtils {private static String driver = null;private static String url = null;private static String username = null;private static String password = null;static {InputStream is = null;try {Properties props = new Properties();is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");props.load(is);driver = props.getProperty("mysql.driver");url = props.getProperty("mysql.url");username = props.getProperty("mysql.username");password = props.getProperty("mysql.password");} catch (IOException e) {e.printStackTrace();throw new RuntimeException();} finally {if (is != null) {try {is.close();} catch (IOException e) {e.printStackTrace();throw new RuntimeException();}}}}/*** 获得连接** @return 连接*/public static Connection getConnection() {Connection ct = null;try {Class.forName(driver);ct = DriverManager.getConnection(url, username, password);} catch (Exception e) {e.printStackTrace();throw new RuntimeException();}return ct;}}
# mysql info
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
mysql.username=root
mysql.password=123456
package com.pc.jdbc.mydatasource;import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;/*** 使用装饰者模式,简单的实现了连接关闭的复写,实现将关闭连接加入连接池** @author Switch* @data 2016年9月30日* @version V1.0*/
public class MyConnection implements Connection {// 1.持有Connection接口和连接池Connection ct = null;LinkedList<Connection> dataSource = null;/*** 通过传入的参数,初始化持有对象** @param ct* 连接* @param dataSource* 连接池*/public MyConnection(Connection ct, LinkedList<Connection> dataSource) {this.ct = ct;this.dataSource = dataSource;}/*** 将装饰后的连接重新加入到连接池中*/@Overridepublic void close() throws SQLException {this.dataSource.add(this);}// 之后的方法,要使用哪个,就实现哪个,如果功能没改变,则可以直接使用持有对象ct的相同调用@Overridepublic PreparedStatement prepareStatement(String arg0) throws SQLException {return this.ct.prepareStatement(arg0);}@Overridepublic boolean isWrapperFor(Class<?> arg0) throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic <T> T unwrap(Class<T> arg0) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void abort(Executor arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void clearWarnings() throws SQLException {// TODO Auto-generated method stub}@Overridepublic void commit() throws SQLException {// TODO Auto-generated method stub}@Overridepublic Array createArrayOf(String arg0, Object[] arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Blob createBlob() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Clob createClob() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic NClob createNClob() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic SQLXML createSQLXML() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Statement createStatement() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Statement createStatement(int arg0, int arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Statement createStatement(int arg0, int arg1, int arg2) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Struct createStruct(String arg0, Object[] arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean getAutoCommit() throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic String getCatalog() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Properties getClientInfo() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic String getClientInfo(String arg0) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic int getHoldability() throws SQLException {// TODO Auto-generated method stubreturn 0;}@Overridepublic DatabaseMetaData getMetaData() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic int getNetworkTimeout() throws SQLException {// TODO Auto-generated method stubreturn 0;}@Overridepublic String getSchema() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic int getTransactionIsolation() throws SQLException {// TODO Auto-generated method stubreturn 0;}@Overridepublic Map<String, Class<?>> getTypeMap() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic SQLWarning getWarnings() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean isClosed() throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean isReadOnly() throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean isValid(int arg0) throws SQLException {// TODO Auto-generated method stubreturn false;}@Overridepublic String nativeSQL(String arg0) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic CallableStatement prepareCall(String arg0) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic CallableStatement prepareCall(String arg0, int arg1, int arg2) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String arg0, String[] arg1) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String arg0, int arg1, int arg2) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void releaseSavepoint(Savepoint arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void rollback() throws SQLException {// TODO Auto-generated method stub}@Overridepublic void rollback(Savepoint arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setAutoCommit(boolean arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setCatalog(String arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setClientInfo(Properties arg0) throws SQLClientInfoException {// TODO Auto-generated method stub}@Overridepublic void setClientInfo(String arg0, String arg1) throws SQLClientInfoException {// TODO Auto-generated method stub}@Overridepublic void setHoldability(int arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setNetworkTimeout(Executor arg0, int arg1) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setReadOnly(boolean arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic Savepoint setSavepoint() throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic Savepoint setSavepoint(String arg0) throws SQLException {// TODO Auto-generated method stubreturn null;}@Overridepublic void setSchema(String arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setTransactionIsolation(int arg0) throws SQLException {// TODO Auto-generated method stub}@Overridepublic void setTypeMap(Map<String, Class<?>> arg0) throws SQLException {// TODO Auto-generated method stub}}
package com.pc.jdbc.mydatasource;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import org.junit.Test;public class TestMyDataSource {@Testpublic void test() {MyDataSource dataSource = new MyDataSource();Connection ct = null;PreparedStatement pstmt = null;ResultSet rs = null;try {ct = dataSource.getConnection();String sql = "select * from users";pstmt = ct.prepareStatement(sql);rs = pstmt.executeQuery();while (rs.next()) {System.out.println(rs.getInt("id") + " " + rs.getString("username") + " " + rs.getString("password"));}} catch (SQLException e) {e.printStackTrace();} finally {try {if (rs != null) {rs.close();}if (pstmt != null) {pstmt.close();}if (ct != null) {ct.close();}} catch (SQLException e) {e.printStackTrace();}}}
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态