重复刷新对我的连接池有影响吗?请高手指点我的连接池代码
如题,请各位大侠指教!
***************************************************
DBConnect.java
**************************************************
package web.common;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class DBConnect {
static private Connection Connection1=null; //数据库连接
private java.sql.Statement stmt = null;
public DBConnect() {
try{
Context ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/mysqlDB");
if (ds != null) {
Connection1= ds.getConnection();
}
}catch(Exception e1) {
e1.printStackTrace();
}
}
public Connection getConn(){
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
}
catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return Connection1;
}
/**
* 用于查询的SQL语句
* @param SQL SQL语句字串 如:select * from tablename
* @return ResultSet 记录集
*/
public ResultSet Query(String SQL)
{
ResultSet rs = null;
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
stmt = Connection1.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(SQL);
}
catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return rs;
}
/**
* 用于查询的SQL语句结果集记录数
* @param SQL SQL语句字串 如:select count(*) from tablename
* @int 记录集
*/
public int Querycount(String SQL)
{
ResultSet rs1 = null;
int number=0;
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
stmt = Connection1.createStatement() ;
rs1 = stmt.executeQuery(SQL);
rs1.next();
number=rs1.getInt(1);
rs1.close();
stmt.close();
//
}
catch( Exception ex )
{
System.err.println("Error in Query - SQLBean : ");
ex.printStackTrace(System.err);
}
return number;
}
/**
* 执行SQL语句,返回结果(ture,false)
* @param SQL
* @return boolean
*/
public boolean execute(String SQL)
{
boolean exeresult=false;
try
{
if((Connection1==null) || (Connection1.isClosed()))
{
new DBConnect();
}
stmt = Connection1.createStatement();
// Connection1.createStatement(ResultSet.,ResultSet.CONCUR_READ_ONLY );
stmt.execute(SQL);
exeresult=true;
}
catch( Exception ex )
{
System.err.println("Error in execute - SQL : ");
ex.printStackTrace(System.err);
}
return exeresult;
}
/**
* 关闭连接
*/
public void closeconn()
{
try
{
if(stmt!=null)
{
stmt.close();
}
if(Connection1!=null)
{
Connection1.close();
}
/* if(ctx!=null)
{
ctx.close();
}
*/
}
catch(Exception e)
{
System.out.println("error is "+e.getMessage());
}
}
}
***************************************************
jsp页面调用
**************************************************
...
...
DBConnect dbc = new DBConnect();
try{
}catche(Exception ec){
out.println(ec.getMessage());
}finally{
dbc.closeconn();
}
...
...