package test;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.*;
import javax.sql.DataSource;
//import javax.sql.rowset.CachedRowSet;
//import com.sun.rowset.*;
import java.sql.ResultSet;
import java.sql.*;
public class ConnDb {
Context initCtx = null;
DataSource ds = null;
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
//CachedRowSet crs = null;
public void ConnDb(){
}
public DataSource getDataSource() throws NamingException{
initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/sqlserver");
return ds;
}
public synchronized Connection getConn() throws NamingException, SQLException{
ds = getDataSource();
conn = ds.getConnection();
return conn;
}
public Statement getStat() throws NamingException, SQLException{
conn = getConn();
stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
return stat;
}
public ResultSet ExecQuery(String strSql){
try{
if(rs != null){
rs.close();
}
if(conn == null){
conn = getConn();
}
if(stat == null){
stat = getStat();
}
//crs = new CachedRowSetImpl();
rs = stat.executeQuery(strSql);
//crs.populate(rs);
}
catch(NamingException nE){
System.out.println("ConnDb.ExecQuery().NamingException:"+nE.getMessage());
}
catch(SQLException sqlE){
System.out.println("ConnDb.ExecQuery().SQLException:"+sqlE.getMessage());
}
finally{
//close();
}
return rs;
}
public int ExecSQL(String sql){
int err=0;
try{
if (conn != null && stat != null)
stat.executeUpdate(sql);
else
throw new SQLException();
}
catch(SQLException sqlE){
err = sqlE.getErrorCode();
}
return err;
}
public int ExecUpdate(String strSql){
return ExecSQL(strSql);
}
public int ExecInsert(String strSql){
return ExecSQL(strSql);
}
public int ExecDelete(String strSql){
return ExecSQL(strSql);
}
public void close(){
try{
if(rs != null){
rs.close();
rs = null;
}
if(stat != null){
stat.close();
stat = null;
}
if(conn != null && conn.isClosed() == false){
conn.close();
conn = null;
}
}
catch(SQLException sqlE){
System.out.println("ConnDb.close().SQLException:"+sqlE.getMessage());
}
}
}
以上是我写的BEAN。我的maxActive设置为10,方便测试:现有几个问题
1。我每次在我写的.java文件里ConnDb conndb = new ConnDb();时,我的ConnDb这个BEAN是从新创建新的数据库连接还是恢复被close调的连接?
2。我在.jsp页里面调用这个bean后每刷新一次就发觉数据库连接多了几个(我的.jsp里面在finally里面调用了close()方法),是否又创建的新的连接?
3。若真是我想的这样,请写过类似BEAN的大虾们给个,参考一下。谢谢了!!
import javax.naming.InitialContext;
import javax.naming.*;
import javax.sql.DataSource;
//import javax.sql.rowset.CachedRowSet;
//import com.sun.rowset.*;
import java.sql.ResultSet;
import java.sql.*;
public class ConnDb {
Context initCtx = null;
DataSource ds = null;
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
//CachedRowSet crs = null;
public void ConnDb(){
}
public DataSource getDataSource() throws NamingException{
initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/sqlserver");
return ds;
}
public synchronized Connection getConn() throws NamingException, SQLException{
ds = getDataSource();
conn = ds.getConnection();
return conn;
}
public Statement getStat() throws NamingException, SQLException{
conn = getConn();
stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
return stat;
}
public ResultSet ExecQuery(String strSql){
try{
if(rs != null){
rs.close();
}
if(conn == null){
conn = getConn();
}
if(stat == null){
stat = getStat();
}
//crs = new CachedRowSetImpl();
rs = stat.executeQuery(strSql);
//crs.populate(rs);
}
catch(NamingException nE){
System.out.println("ConnDb.ExecQuery().NamingException:"+nE.getMessage());
}
catch(SQLException sqlE){
System.out.println("ConnDb.ExecQuery().SQLException:"+sqlE.getMessage());
}
finally{
//close();
}
return rs;
}
public int ExecSQL(String sql){
int err=0;
try{
if (conn != null && stat != null)
stat.executeUpdate(sql);
else
throw new SQLException();
}
catch(SQLException sqlE){
err = sqlE.getErrorCode();
}
return err;
}
public int ExecUpdate(String strSql){
return ExecSQL(strSql);
}
public int ExecInsert(String strSql){
return ExecSQL(strSql);
}
public int ExecDelete(String strSql){
return ExecSQL(strSql);
}
public void close(){
try{
if(rs != null){
rs.close();
rs = null;
}
if(stat != null){
stat.close();
stat = null;
}
if(conn != null && conn.isClosed() == false){
conn.close();
conn = null;
}
}
catch(SQLException sqlE){
System.out.println("ConnDb.close().SQLException:"+sqlE.getMessage());
}
}
}
以上是我写的BEAN。我的maxActive设置为10,方便测试:现有几个问题
1。我每次在我写的.java文件里ConnDb conndb = new ConnDb();时,我的ConnDb这个BEAN是从新创建新的数据库连接还是恢复被close调的连接?
2。我在.jsp页里面调用这个bean后每刷新一次就发觉数据库连接多了几个(我的.jsp里面在finally里面调用了close()方法),是否又创建的新的连接?
3。若真是我想的这样,请写过类似BEAN的大虾们给个,参考一下。谢谢了!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货