JDBC学习(六)JDBC数据源的简单实现(使用代理模式proxy pattern)

<p>1,ArrayList 和linkedList的区别。ArrayList使用数组实现,访问数组很快,LinkedList使用链表来实现,增加删除速度很快。</p>

<p><textarea cols="67" rows="15" name="code" class="java">package cn.itcast.jdbc.datasource;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Map;

/**
*
* 2008-12-13
*
* @author <a href="mailto:liyongibm@gmail.com" mce_href="mailto:liyongibm@gmail.com">liyong</a>
*
*/
public class MyConnection implements Connection {
private Connection realConnection;
private MyDataSource2 dataSource;
private int maxUseCount = 5;
private int currentUserCount = 0;

MyConnection(Connection connection, MyDataSource2 dataSource) {
this.realConnection = connection;
this.dataSource = dataSource;
}

public void clearWarnings() throws SQLException {
this.realConnection.clearWarnings();
}

public void close() throws SQLException {
this.currentUserCount++;
if (this.currentUserCount < this.maxUseCount)
this.dataSource.connectionsPool.addLast(this);
else {
this.realConnection.close();
this.dataSource.currentCount--;
}
}

public void commit() throws SQLException {
this.realConnection.commit();
}

public Statement createStatement() throws SQLException {
return this.realConnection.createStatement();
}

public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public boolean getAutoCommit() throws SQLException {
// TODO Auto-generated method stub
return false;
}

public String getCatalog() throws SQLException {
// TODO Auto-generated method stub
return null;
}

public int getHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}

public DatabaseMetaData getMetaData() throws SQLException {
// TODO Auto-generated method stub
return null;
}

public int getTransactionIsolation() throws SQLException {
// TODO Auto-generated method stub
return 0;
}

public Map<String, Class<?>> getTypeMap() throws SQLException {
// TODO Auto-generated method stub
return null;
}

public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}

public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}

public boolean isReadOnly() throws SQLException {
// TODO Auto-generated method stub
return false;
}

public String nativeSQL(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}

public CallableStatement prepareCall(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}

public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}

public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public PreparedStatement prepareStatement(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}

public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
// TODO Auto-generated method stub
return null;
}

public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public void releaseSavepoint(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub

}

public void rollback() throws SQLException {
// TODO Auto-generated method stub

}

public void rollback(Savepoint savepoint) throws SQLException {
// TODO Auto-generated method stub

}

public void setAutoCommit(boolean autoCommit) throws SQLException {
// TODO Auto-generated method stub

}

public void setCatalog(String catalog) throws SQLException {
// TODO Auto-generated method stub

}

public void setHoldability(int holdability) throws SQLException {
// TODO Auto-generated method stub

}

public void setReadOnly(boolean readOnly) throws SQLException {
// TODO Auto-generated method stub

}

public Savepoint setSavepoint() throws SQLException {
// TODO Auto-generated method stub
return null;
}

public Savepoint setSavepoint(String name) throws SQLException {
// TODO Auto-generated method stub
return null;
}

public void setTransactionIsolation(int level) throws SQLException {
// TODO Auto-generated method stub

}

public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
// TODO Auto-generated method stub

}

}
</textarea></p>

<p><textarea cols="67" rows="15" name="code" class="java">package cn.itcast.jdbc.datasource;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

import javax.sql.DataSource;

/**
*
* 2008-12-13
*
* @author <a href="mailto:liyongibm@gmail.com" mce_href="mailto:liyongibm@gmail.com">liyong</a>
*
*/
public class MyDataSource2 implements DataSource{
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "";

private static int initCount = 1;
private static int maxCount = 1;
int currentCount = 0;

LinkedList<Connection> connectionsPool = new LinkedList<Connection>();

public MyDataSource2() {
try {
for (int i = 0; i < initCount; i++) {
this.connectionsPool.addLast(this.createConnection());
this.currentCount++;
}
} catch (SQLException e) {
throw new ExceptionInInitializerError(e);
}
}

public Connection getConnection() throws SQLException {
synchronized (connectionsPool) {
if (this.connectionsPool.size() > 0)
return this.connectionsPool.removeFirst();

if (this.currentCount < maxCount) {
this.currentCount++;
return this.createConnection();
}

throw new SQLException("已没有链接");
}
}

public void free(Connection conn) {
this.connectionsPool.addLast(conn);
}

private Connection createConnection() throws SQLException {
Connection realConn = DriverManager.getConnection(url, user, password);
// MyConnection myConnection = new MyConnection(realConn, this);
// return myConnection;
MyConnectionHandler proxy = new MyConnectionHandler(this);
return proxy.bind(realConn);
}

public Connection getConnection(String username, String password)
throws SQLException {
// TODO Auto-generated method stub
return null;
}

public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}

public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}

public void setLogWriter(PrintWriter out) throws SQLException {
// TODO Auto-generated method stub

}

public void setLoginTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub

}
}
</textarea></p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值