tomcat6+sql2000 连接数据库报“Cannot get a connection,pool exhausted ”错误,怎么解决?
连接数据库的代码:
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DBjdbc{
private DBjdbc(){}
////查询
public static ResultSet querySQL(String sql,String dbname){
Context ctx;
Context envctx;
DataSource ds;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
ctx = new InitialContext();
envctx = (Context)ctx.lookup("java:comp/env");
ds = (DataSource)envctxlookup("java/"+dbname);
conn = ds.getConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
}catch(exception e){
e.printStackTrace();
} finally {
try {
if (rs != null) {rs.close(); }
if (st != null) {st.close(); }
if (conn != null) {conn.close(); }
} catch (SQLException e) {
/** Exception handling **/
}
}
return rs;
}
///// 修改
public static int updateResult(String sql,String dbname)throws SQLException{
Context ctx;
Context envctx;
DataSource ds;
Statement st = null;
Connection conn = null;
try{
ctx = new InitialContext();
envctx = (Context)ctx.lookup("java:comp/env");
ds = (DataSource)envctxlookup("java/"+dbname);
conn = ds.getConnection();
st = conn.createStatement();
}catch(exception e){
e.printStackTrace();
}finally {
try {
if (st != null) {st.close(); }
if (conn != null) {conn.close(); }
} catch (SQLException e) {
/** Exception handling **/
}
}
return rs.executeUpdate(sql);
}
}
配置内容为:
<Resource name="jdbc/sys"
auth="Container"
type="javax.sql.DataSource"
maxActive="0"
maxIdle="30"
maxWait="10000"
username="sys"
password="guanyitongcn"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://221.222.111.333:1433;DatabaseName=sys"/>
加个关闭连接,也就是带颜色的那部分,提示:Object has been closed.
不加的话,连接多了,又抛错说:cannot get a connection,pool exhausted