请教:jsp使用oracle数据库的问题
灵智上人 2004-09-03 09:06:23 我用的是resin做web服务器,在resin中对数据库的连接是这样设置的
<resource-ref>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<init-param driver-name="oracle.jdbc.driver.OracleDriver"/>
<init-param url="jdbc:oracle:thin:@localhost:1521:orcl"/>
<init-param user="crmdbo"/>
<init-param password="crmdbo"/>
<init-param max-connections="1000"/>
<init-param max-idle-time="30"/>
</resource-ref>
执行对数据库操作的bean是这样写的
import javax.naming.*;
import java.sql.*;
import javax.sql.*;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.DriverManager;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.util.ArrayList;
public class ConnDb
{
Connection conn=null;
Statement stmt=null;
DataSource pool;
/*初始化*/
public void init()
{
try
{
Context env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource) env.lookup("jdbc/test");
} catch (NamingException e) {
}
}
/*创建连接*/
public Connection getConnection()
{
init();
Connection conn;
try
{
conn = pool.getConnection();
return conn;
}catch (Exception e){
System.out.println(e);
return null;
}
}
/*执行sql语句,返回ResultSet*/
public ResultSet executeQuery(String sqlstr)
{
try
{
conn = getConnection();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlstr);
return rs;
}catch(Exception e){
try
{
conn.close();
stmt.close();
}catch(Exception ee){
System.out.println(ee);
}
return null;
}
}
public void close()
{
try
{
stmt.close();
conn.close();
}catch(Exception ee){
System.out.println(ee);
try
{
conn.close();
stmt.close();
}catch(Exception ex){
System.out.println(ex);
}
}
}
/*执行sql语句*/
public int executeUpdate(String sqlstr)
{
try
{
conn = getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sqlstr);
stmt.close();
conn.close();
return 1;
}catch(Exception e){
try{
stmt.close();
conn.close();
System.out.println(e);
}catch(Exception ee){
System.out.println(ee);
}
return 0;
}
}
private Hashtable getTheValue(ResultSet rs, ResultSetMetaData rsmd)
{
/*********从结果集中取出一条记录****************/
Hashtable hstab = new Hashtable();
try
{
if(rs != null){
int maxrows = rsmd.getColumnCount();
for (int i = 1; i <= maxrows; i++){
String key = rsmd.getColumnName(i);
String value = rs.getString(i);
if (value == null){
value = "";
}
hstab.put(key, value);
}
}
rs.close();
}catch(Exception e){
System.out.println(e.getMessage());
try
{
rs.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
return hstab;
}
return hstab;
}
public ArrayList getList(String sqlString)
{
/*********查询结果集,返回ArrayList***********/
ArrayList pkv = new ArrayList();
try
{
conn = getConnection();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlString);
ResultSetMetaData rsmd = rs.getMetaData();
int num = rsmd.getColumnCount();
while (rs.next())
{
Hashtable table = new Hashtable();
for (int i = 1;i <= num; i++)
{
String key = rsmd.getColumnName(i);
String value = rs.getString(i);
if (value==null)
value = "";
table.put(key,value);
}
pkv.add(table);
}
rs.close();
stmt.close();
conn.close();
}catch (SQLException e){
System.out.println(e);
try{
stmt.close();
conn.close();}
catch(Exception ee)
{
System.out.println(ee);
}
}
return pkv;
}
public Hashtable getValue(String sql)
{
/***************获取一条记录*******************/
Hashtable hstab = new Hashtable();
Connection con = getConnection();
Statement stmt = null;
try{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
if (rs.next()){
hstab = this.getTheValue(rs, rsmd);
}
rs.close();
stmt.close();
con.close();
return hstab;
}catch(Exception e){
System.out.println(e.getMessage());
try{
stmt.close();
con.close();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return hstab;
}
}
请问为什莫对数据库操作时,数据库中的线程不断增加
当操作到一定程度是,resin服务就会报超出数据库进程的错误,只有在关闭resin后数据库中的线程才会被终止
有什莫解决办法