兄弟,看你的程序,你每次调用方法executeQuery、executeCommand都创建了一个连接,即
rs=dbc.executeQuery(seln);//这里和后面都有
也就是说即使你rs.Close了,但connect对象仍然存在,即资源没有被释放,当然会越来越慢了
改变一下你的程序结构吧,connect只需连接一次即可
rs=dbc.executeQuery(seln);//这里和后面都有
也就是说即使你rs.Close了,但connect对象仍然存在,即资源没有被释放,当然会越来越慢了
改变一下你的程序结构吧,connect只需连接一次即可
解决方案 »
- 对象调用方法后会怎么样?是否可以嵌套调用方法
- 这么简单的题,上机调试就出错了,哪位朋友帮我看看。
- 如何继承多个类
- 简单问题的算法 求解
- 我刚装好,是不是需要配制环境变量?求教。
- java中,swing 和 awt 有什么区别?
- 31号考SCJP...大送分了!!!
- **送给总爱问java能做什么的一些人..........
- 求inside the java virtual machine second edtion 这本书 英文版
- java 线程问题
- jbuilder中如何实现使打开的一个新的Frame窗口最大化???
- 帮忙解决:乱码问题(环境redhat linux + tomcat)今天结账(2002-9-10)
conn = DriverManager.getConnection(sConnStr,DataUserName,DataPassWord);
//Statement stmt=conn.createStatement();
Statement stmt=conn.createStatement();
这部分以前的代码均可以放到一块,然后需要执行查询等操作的时候,将stmt作为参数之一传递进去即可
比如
public void executeCommand(String sql)
改为:
public void executeCommand(Statement stmt,String sql)
同时,建议把你的Connection放在连接池中,这样可避免每次都要连接数据库
conn.close()
时时!
private int checkedOut=0;
private Vector freeConnections = new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user; /**
* 创建新的连接池
*
* @param name 连接池名字
* @param URL 数据库的JDBC URL
* @param user 数据库帐号,或 null
* @param password 密码,或 null
* @param maxConn 此连接池允许建立的最大连接数
*/
public DBConnectionPool(String name, String URL, String user, String password,int maxConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
} /**
* 将不再使用的连接返回给连接池
*
* @param con 客户程序释放的连接
*/
public synchronized void freeConnection(Connection con) {
// 将指定连接加入到向量末尾
freeConnections.addElement(con);
checkedOut--;
notifyAll();
} /**
* 从连接池获得一个可用连接.如没有空闲的连接且当前连接数小于最大连接
* 数限制,则创建新连接.如原来登记为可用的连接不再有效,则从向量删除之,
* 然后递归调用自己以尝试新的可用连接.
*/
public synchronized Connection getConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
// 获取向量中第一个可用连接
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log("从连接池" + name+"删除一个无效连接");
// 递归调用自己,尝试再次获取可用连接
con = getConnection();
}
}
catch (SQLException e) {
log("从连接池" + name+"删除一个无效连接");
// 递归调用自己,尝试再次获取可用连接
con = getConnection();
}
}
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
else{
this.release();
con = null;
}
if (con != null) {
checkedOut++;
}
return con;
} /**
* 从连接池获取可用连接.可以指定客户程序能够等待的最长时间
* 参见前一个getConnection()方法.
*
* @param timeout 以毫秒计的等待时间限制
*/
public synchronized Connection getConnection(long timeout) {
long startTime = new Date().getTime();
Connection con;
while ((con = getConnection()) == null) {
try {
wait(timeout);
} catch (InterruptedException e) {}
if ((new Date().getTime() - startTime) >= timeout) {
// wait()返回的原因是超时
return null;
}
}
return con;
} /**
* 关闭所有连接
*/
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log("关闭连接池" + name+"中的一个连接");
} catch (SQLException e) {
log(e, "无法关闭连接池" + name+"中的连接");
}
}
freeConnections.removeAllElements();
} /**
* 创建新的连接
*/
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
} else {
con = DriverManager.getConnection(URL, user, password);
}
log("连接池" + name+"创建一个新的连接");
} catch (SQLException e) {
log(e, "无法创建下列URL的连接: " + URL);
return null;
}
return con;
}
}