首先我是用JAVA编写了查询Oracle数据库的类,然后在JSP里面去调用这个类,显示查询结果。但在用SQL语句(select count(*) v$session)查数据库的链接时,数量一直时增加,但其中大部分都是刚才的死链接。怎样才能使链接数不增加呢?请大家帮帮我。前提是在JAVA里我已经写了关闭链接的方法!
链接数据库的类(DBConnection):
public static Connection getConn()
{
String user = "a";
String password = "a";
String url = "jdbc:oracle:thin:@10.1.1.1:1521:1521:ora90";
Connection conn = null;
try
{
Properties props = new Properties();
props.put("user", user);
props.put("password", password);
Driver myDriver = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = myDriver.connect("jdbc:oracle:thin:@10.1.1.1:1521:ora90", props);
conn.setAutoCommit(false);
System.out.println("connect sueess");
}
catch(Exception e)
{
System.out.println("Exception was thrown: " + e.getMessage());
}
return conn;
}
多线程的类:
public synchronized void FormatOutput(int count_pre)throws Exception
{
//定义sql数组
String []al_array = new String[162];
//返回时用
ArrayList al_return = new ArrayList();
//取系统时间
Date utilDate = new Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getYear(),utilDate.getMonth(),utilDate.getDay()); try
{
//初始化数据库
initDatabase("");
//从数组中取得sql语句
String sql = this.sql_Array[count_pre-1];
//改变记数基数
count_pre = count_pre*2-1;
//执行两次sql语句
for(int count = 0; count<2; count++)
{
//设置ps
ps = conn.prepareStatement(sql);
ps = this.setPreparedStatement(ps,count_pre);
//得到结果集
ResultSet rs = ps.executeQuery();
//得到所有权利
while(rs.next())
{
//把结果放到数组里
String total = String.valueOf(rs.getLong("count(*)"));
String area = String.valueOf(rs.getFloat("sum(y.建筑面积)"));
//String price = String.valueOf(rs.getFloat("sum(y.合同总价)"));
//将合同总价字段的值变为float long
float price_zj = rs.getFloat("sum(y.合同总价)");
Float price_f=new Float(price_zj);
long price_f_long=price_f.longValue() ;
String price=String.valueOf(price_f_long);
this.result_Array[count_pre*3-3] = total;
this.result_Array[count_pre*3-2] = area;
his.result_Array[count_pre*3-1] = price;
count_pre = count_pre+1;
}
//关闭资源
rs.close();
ps.close();
}
//释放连接
if(null != conn)
conn.close();
}
catch(SQLException e)
{
System.out.println(e);
destroyDatabase();
throw e;
}
}
链接数据库的类(DBConnection):
public static Connection getConn()
{
String user = "a";
String password = "a";
String url = "jdbc:oracle:thin:@10.1.1.1:1521:1521:ora90";
Connection conn = null;
try
{
Properties props = new Properties();
props.put("user", user);
props.put("password", password);
Driver myDriver = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = myDriver.connect("jdbc:oracle:thin:@10.1.1.1:1521:ora90", props);
conn.setAutoCommit(false);
System.out.println("connect sueess");
}
catch(Exception e)
{
System.out.println("Exception was thrown: " + e.getMessage());
}
return conn;
}
多线程的类:
public synchronized void FormatOutput(int count_pre)throws Exception
{
//定义sql数组
String []al_array = new String[162];
//返回时用
ArrayList al_return = new ArrayList();
//取系统时间
Date utilDate = new Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getYear(),utilDate.getMonth(),utilDate.getDay()); try
{
//初始化数据库
initDatabase("");
//从数组中取得sql语句
String sql = this.sql_Array[count_pre-1];
//改变记数基数
count_pre = count_pre*2-1;
//执行两次sql语句
for(int count = 0; count<2; count++)
{
//设置ps
ps = conn.prepareStatement(sql);
ps = this.setPreparedStatement(ps,count_pre);
//得到结果集
ResultSet rs = ps.executeQuery();
//得到所有权利
while(rs.next())
{
//把结果放到数组里
String total = String.valueOf(rs.getLong("count(*)"));
String area = String.valueOf(rs.getFloat("sum(y.建筑面积)"));
//String price = String.valueOf(rs.getFloat("sum(y.合同总价)"));
//将合同总价字段的值变为float long
float price_zj = rs.getFloat("sum(y.合同总价)");
Float price_f=new Float(price_zj);
long price_f_long=price_f.longValue() ;
String price=String.valueOf(price_f_long);
this.result_Array[count_pre*3-3] = total;
this.result_Array[count_pre*3-2] = area;
his.result_Array[count_pre*3-1] = price;
count_pre = count_pre+1;
}
//关闭资源
rs.close();
ps.close();
}
//释放连接
if(null != conn)
conn.close();
}
catch(SQLException e)
{
System.out.println(e);
destroyDatabase();
throw e;
}
}
CONN用一个不行吗?