我想把连接池的调用放在bean里面,但我不知道怎么返回池连接,帮我看看

02-09-05 jserkang

我想把连接池的调用放在这个bean里面,但我不知道怎么返回池连接,conn.close不管用,但在jsp程序里却可以,如果用我现在的bean来执行数据库程序,只能执行5次(我的weblogic默认池连接数为5),5个用完,就over了,谁能告诉我在bean里怎么把池连接返回。谢谢你们了

package test;

import java.sql.*;

import java.util.*;

import javax.naming.*;

import javax.sql.*;

public class bbsreg{

Connection conn = null;

ResultSet rs = null;

Statement stmt =null;

Context ctx = null;

javax.sql.DataSource ds=null;

public bbsreg() {

try

{

Hashtable ht = new Hashtable();

ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");

ht.put(Context.PROVIDER_URL,"t3://127.0.0.1:7001"); //此处为WEBLOGIC服务器地址和端口

Context ctx = new InitialContext(ht);

ds = (javax.sql.DataSource) ctx.lookup ("dsdata"); //此处为数据源中JNDI的名称。

conn = ds.getConnection();

}

catch(Exception e) {

System.err.println("bbsreg(): " + e.getMessage());

}

}

public ResultSet executeQuery(String sql) {

rs = null;

try {

Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);

rs = stmt.executeQuery(sql);

}

catch(SQLException ex) {

System.err.println("aq.executeQuery: " + ex.getMessage());

System.err.println("aq.executeQuerystrSQL: " + sql);

}

return rs;

}

public void executeUpdate(String sql) {

try {

Statement stmt = conn.createStatement();

stmt.executeUpdate(sql);

}

catch(SQLException ex) {

System.err.println("aq.executeUpdate: " + ex.getMessage());

System.err.println("aq.executeUpadatestrSQL: " + sql);

}

}

public boolean closeConn()

{

try

{

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

if(conn!=null)

conn.close();

return true;

}

catch(SQLException ex)

{

System.out.println("closeConn:"+ex.getMessage());

return false;

}

finally {

try {ctx.close();

}

catch (Exception e) {

// a failure occurred

}

}

}

}