package com.xk.jdbc.datasource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.sql.DataSource;
import com.xk.jdbc.utils.JDBCUtil;
/**
* @author: XuKe
* @time :2018年11月17日 下午9:17:22
*
*/
public class MyDataSource implements DataSource {
//将一些连接存入内存中,可以定义一个集合,用于存储连接对象。
private List connList = new ArrayList();
//在初始化的时候提供一些连接方法
public MyDataSource() {
//初始化连接
for(int i= 0;i <=3;i++) {
connList.add(JDBCUtil.getConn());
}
}
//从连接池中获得一些连接的方法
@Override
public Connection getConnection() throws SQLException {
Connection conn = connList.remove(0);
return conn;
}
//自己写的归还连接对象的方法
public void addBack(Connection conn) {
connList.add(conn);
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
// TODO Auto-generated method stub
return null;
}
@Override
public T unwrap(Class iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
工具类的抽取:
package com.xk.jdbc.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author: XuKe
* @time :2018年11月16日 下午3:38:00
*
*/
public class JDBCUtil {
static String driverClass;
static String url;
static String name;
static String password;
static {
try {
Properties properties = new Properties();
/*InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
pro.load(is);*/
properties.load(new FileInputStream("src/jdbc.properties"));
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties .getProperty("password");
} catch (IOException e) {
System.out.println("读取配置文件失败");
}
}
/**
* @Title: getConn
* @Description: TODO(创建一个连接对象)
* @return
* @return Connection
*/
public static Connection getConn() {
Connection conn = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, name, password);
} catch (SQLException e) {
System.out.println("创建连接对象失败");
} catch (ClassNotFoundException e) {
System.out.println("注册失败");
}
return conn;
}
/**
* @Title: release
* @Description: TODO(关闭增删改的对象)
* @param stmt
* @param conn
* @return void
*/
public static void release(Statement stmt,Connection conn) {
closeStmt(stmt);
closeConn(conn);
}
/**
* @Title: release
* @Description: TODO(关闭查找的对象)
* @param rs
* @param stmt
* @param conn
* @return void
*/
public static void release(ResultSet rs,Statement stmt,Connection conn) {
closeRs(rs);
closeStmt(stmt);
closeConn(conn);
}
/**
* @Title: release
* @Description: TODO(释放并归还连接对象)
* @param stmt
* @return void
*/
public static void release(Statement stmt) {
closeStmt(stmt);
}
/**
* @Title: release
* @Description: TODO(释放并归还连接对象)
* @param rs
* @param stmt
* @return void
*/
public static void release(ResultSet rs,Statement stmt) {
closeRs(rs);
closeStmt(stmt);
}
/**
* @Title: closeRs
* @Description: TODO(关闭结果集)
* @param rs
* @return void
*/
private static void closeRs(ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs = null;
}
}
/**
* @Title: closeStmt
* @Description: TODO(关闭stmt)
* @param stmt
* @return void
*/
private static void closeStmt(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
stmt = null;
}
}
/**
* @Title: closeConn
* @Description: TODO(关闭连接对象)
* @param conn
* @return void
*/
private static void closeConn(Connection conn) {
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn = null;
}
}
}
测试:
package com.xk.jdbc.datasource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.xk.jdbc.utils.JDBCUtil;
/**
* @author: XuKe
* @time :2018年11月18日 下午4:40:36
*
*/
public class MyDataSourceTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
MyDataSource myDataSource = null;
try {
//1获得连接对象
//conn= JDBCUtil.getConn();
myDataSource = new MyDataSource();
conn = myDataSource .getConnection();
String sql = "select * from account";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name") + " "+ rs.getDouble("money") );
}
} catch (SQLException e) {
System.out.println("预处理错误");
}finally {
//释放
//JDBCUtil.release(rs, pstmt, conn);
myDataSource.addBack(conn);
JDBCUtil.closeRs(rs);
JDBCUtil.closeStmt(pstmt);
}
}
}
MyDataSource dataSource = new MyDataSource();
// 归还连接:
dataSource.addBack(conn);
如果不提供自定义的方法就可以解决这个问题,但是连接要如何归还到连接池呢?
原来在Connection中是有一个close方法的,colse方法完成了连接的销毁。能不能做一个事情,将原有的连接的close方法改为归还。
class Man{
public void run(){
System.out.println(“跑…”);
}
}
class SuperMan extends Man{
public void run(){
System.out.println(“飞…”);
}
}
interface Waiter{
public void server();
}
public class Waitress implements Waiter{
public void server(){
System.out.println(“服务中…”);
}
}
public class WaitressWrapper implements Waiter{
private Waiter waiter;
public WaitressWrapper(Waiter waiter){//用于接收 Waitress的对象
this.waiter = waiter;
}
public void server(){
System.out.println(“微笑…”)
waiter.server();
}
}
为了简化编程,提供一个模板类(模板类原封不动的将接口中的所有方法都实现,但是都没有增强)。编写一个装饰类继承模板类。在装饰类中只需要增强某一个方法即可。