我用tomcat连接池连接数据库,过一天就提示Cannot get a connection, pool exhausted,查找资料得到的是连接池已经用完
import java.sql.*;
import java.io.*;
import javax.naming.*;
import javax.sql.*;
public class pubconn {
private String url="jdbc:JSQLConnect://localhost/database=prosonjob1";
private String sDBDriver = "com.jnetdirect.jsql.JSQLDriver";//
//private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=prosonjob1";
// private String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private ResultSet rs = null;
// private String user="sa";
// private String password="123456789";
public pubconn()
{
try{
//Class.forName(sDBDriver);
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
// Context ctx = (Context) initCtx.lookup("java:comp/env");
// Object obj = (Object) ctx.lookup("prosenjoblink");
javax.sql.DataSource ds = (javax.sql.DataSource)initCtx.lookup("java:comp/env/jdbc/personjob");
System.out.println(ds);
conn = ds.getConnection();
}catch(Exception ex)
{
System.err.println("连接数据库加载驱动程序中发生错误"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
//conn =DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
rs = stmt.executeQuery(sql);
}catch(Exception ex)
{
System.err.println("执行数据库查询使发生错误: " + ex.getMessage());
}
return rs;
}
public boolean executeUpdate(String sql)
{
boolean bupdate=false;
//rs = null;
try{
//conn =DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int rowCount = stmt.executeUpdate(sql);
if(rowCount!=0)bupdate=true;
}
catch(Exception ex) {
System.err.println("更新数据库时间发生错误: " + ex.getMessage());
}
return bupdate;
}
/**
* PrepareStatement(更新和查询)
*/
public pubconn(String sql) throws Exception{
// Class.forName(sDBDriver);
// conn=DriverManager.getConnection(url,user,password);
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
//Context ctx = (Context) initCtx.lookup("java:comp/env");
/// Object obj = (Object) ctx.lookup("prosenjoblink");
javax.sql.DataSource ds = (javax.sql.DataSource)initCtx.lookup("java:comp/env/jdbc/personjob");
System.out.println(ds);
conn = ds.getConnection();
this.prepareStatement(sql);
}
public Connection getConnection(){
return conn;
}
//创建访问数据库的对象
public void prepareStatement(String sql) throws Exception{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
prepstmt = conn.prepareStatement(sql);//?注意和stmt的区别
}
//设置参数(字符串)
public void setString(int index,String value) throws Exception{
value=new String(value.getBytes("GBK"),"ISO8859_1");
prepstmt.setString(index,value);
}
//(整数)
public void setInt(int index,int value) throws Exception{
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws Exception {
prepstmt.setBoolean(index,value);
}
//(时间)
public void setDate(int index,Date value) throws Exception {
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws Exception {
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws Exception {
prepstmt.setFloat(index,value);
}
public void setBinaryStream(int index,InputStream in,int length) throws Exception {
prepstmt.setBinaryStream(index,in,length);
}
public void clearParameters() throws Exception{
prepstmt.clearParameters();
}
public PreparedStatement getPreparedStatement(){
return prepstmt;
}
//更新
public void executeUpdate() throws Exception {
if (prepstmt != null){
prepstmt.executeUpdate();
}
}
//查询
public ResultSet executeQuery() throws Exception {
if (prepstmt != null) {
rs=prepstmt.executeQuery();
return rs;
}
else return null;
}
public String toChinese(String str){
if(str==null||str.length()<1){
str="";
}else{
try {
str=(new String(str.getBytes("iso-8859-1"),"GB2312"));
} catch (UnsupportedEncodingException e) {
System.err.print(e.getMessage());
e.printStackTrace();
return str;
}
}
return str;
}
/**
* 关闭数据库
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null){
prepstmt.close();
prepstmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}
}
我的机器上怎么刷都是正常的?不知道是什么原因