liyanguo01 2014年01月18日
本人新手,请问java 操作sql数据库 数据库连接数持续上升导致死机。怎样释放连接数
本人新手,请问java 操作sql数据库 数据库连接数持续上升导致死机。程序调用ResultSet dbquery怎样释放连接数
谢谢 请帮忙写个释放代码
package dbcon;
import java.sql.*;
import java.net.*;
import java.util.StringTokenizer;
import java.util.*;
import java.io.IOException;
import java.io.PrintStream;
import java.io.*;
import javax.servlet.http.*;
public class dbcon{
private static String driverstr=" ";
private static String user=" ";
private static String password=" ";
private String databasename=" ";
private static String midname="";
private static String errorbz=" ";
private static String url=" "; //"jdbc:microsoft:sqlserver://10.66.39.134:1433;DatabaseName=test";
ResultSet rs;
String vhostipaddress="";
private HttpSession mysession;

public int dbtest()
{
try{

Connection conn=getConn();
return 1;
}catch(Exception e)
{
System.out.print(e.toString());
return 0;
}
}
public int setSession( HttpSession rq)
{
mysession=rq;

return 1;
}
public int putsession()
{
return 1;
}
public void dbexe(String sqlstr)
{
try{

Connection conn= getConn();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.execute(sqlstr);
errorbz="0";
}catch(Exception e)
{
System.out.println(e.toString());
errorbz="1";
}
}
public String getErrorbz()
{
return errorbz;
}

public ResultSet dbquery(String sqlstr)
{
try{

Connection conn=getConn();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sqlstr);
}
catch(Exception e){
System.out.println(e.toString());
return null;
}
return rs;
}

public void setDatabasename(String value)
{

System.out.println("before"+databasename);
String ax=value;
String bx=ax.substring(0,6);
String cx=ax.substring(6,11);
int rt=0;
if(driverstr.trim().length()==0)
try {
rt=getxml();
databasename=bx+midname.trim()+cx;
}
catch(Exception e)
{
databasename="";
}

databasename=bx+midname.trim()+cx;
System.out.println("after"+databasename);
//databasename=value;
}
public String getDatabasename()
{
return databasename;
}

public int dbcheck(String checkstr,String strcorpcode)
{
int errorrowindex=-1;
int i;
try{
StringTokenizer st = new StringTokenizer(checkstr,"~~~");
String arr[] = new String[st.countTokens()];
for (i=0; st.hasMoreTokens(); i++)
{
arr[i] = st.nextToken();
}


Connection conn= getConn();

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

for (int j=0; j<i; j++)
{
rs=stmt.executeQuery("select * from wzzl where (corpcode='"+strcorpcode+"' or (groupflag=1 and wzkkbz=1)) and wzbh='"+arr[j]+"'");
if(!rs.next())
{
return j;
}
}
//rs=stmt.executeQuery(sqlstr);

}
catch(Exception e){
System.out.println(e.toString());
}
return errorrowindex;
}


public int numQry(String sqlstr) //gogogo
{
int numRst=0; //记录结果条数

try{

Connection conn=getConn();
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sqlstr);

while(rs.next()){ //未经验证$$$$$$$$$$
numRst++;
}

}
catch(Exception e){
System.out.println(e.toString());
}
return numRst;
}
public int getxml() throws Exception
{
// Document doc;
// Element root;
// List confs;
//URL rL;
byte[] ibt=new byte[500];
int L=0;
String sbt="";
String fname="/web-inf/wzpscon.xml";
fname=mysession.getServletContext().getRealPath(fname);

FileInputStream fi=new FileInputStream(fname);
L=fi.read(ibt);
fi.close();
sbt=new String(ibt,0,L);
String sep[]={"<url>","</url>","<class>","</class>","<usr>","</usr>","<pwd>","</pwd>","<database>","</database>","<midname>","</midname>"};
int[] ii=new int[sep.length];
L=sep.length;
int i=0;
int i0=sbt.indexOf("<hhwl>");
int i9=sbt.indexOf("</hhwl>");
if (i0>=0 && i9>i0)
i=0;
int er=0;
while(i<L)
{
ii[i]=sbt.indexOf(sep[i]);
if(ii[i]==-1)
{ er=1;
break;
}
ii[i]+=sep[i].length();
i++;
ii[i]=sbt.indexOf(sep[i]);
if(ii[i]<ii[i-1])
{
er=1;
break;
}
i++;


}
if(er==1)
return -1;
url=sbt.substring(ii[0],ii[1]);
driverstr=sbt.substring(ii[2],ii[3]);
user=sbt.substring(ii[4],ii[5]);
password=sbt.substring(ii[6],ii[7]);
if(databasename.length()==0)
databasename=sbt.substring(ii[8],ii[9]);
midname=sbt.substring(ii[10],ii[11]);
return 0;
}
//-----------------------------------------------
public Connection getConn(String myDriver,String purl, String dbName, String user, String password)
throws Exception, InstantiationException, IllegalAccessException, SQLException, ClassNotFoundException
{
Class.forName(myDriver).newInstance();
String url =purl + ";DatabaseName=" + dbName;
System.out.println("getConn(,,,)"+purl+dbName);
Connection _conn = DriverManager.getConnection(url, user, password);
return _conn;
}
//----------------------------------------------------
public Connection getConn()
throws Exception, SQLException
{


return getConn(driverstr,url,databasename,user,password);

}
//----------------------------------
public Connection getConnT(String time1)
throws Exception, SQLException
{
String gooDbname="xjktlk_"+time1.trim();
setDatabasename(gooDbname);
return getConn();

}
public Connection getConnDB(String dbname)
throws Exception, SQLException
{
String gooDbname=dbname;
setDatabasename(gooDbname);
return getConn();

}
//-------------------------------
public Statement getStmt()
throws Exception, SQLException
{
Connection _conn = getConn();
Statement _stmt = _conn.createStatement();
System.out.println("getStmt()");
return _stmt;
}

public Statement getStmt(Connection _conn)
throws Exception, SQLException
{
Statement _stmt = _conn.createStatement();
System.out.println("getStmt(_conn)");
return _stmt;
}

public Statement getStmt(int resultSetType, int resultSetConcurrency)
throws Exception, SQLException
{
Connection _conn = getConn();
Statement _stmt = _conn.createStatement(resultSetType, resultSetConcurrency);
return _stmt;
}

public Statement getStmt(Connection _conn, int resultSetType, int resultSetConcurrency)
throws Exception, SQLException
{
Statement _stmt = _conn.createStatement(resultSetType, resultSetConcurrency);
return _stmt;
}

public PreparedStatement getPreStmt(Connection _conn, String sqltxt, int resultSetType, int resultSetConcurrency)
throws Exception, SQLException
{
PreparedStatement _prestmt = _conn.prepareStatement(sqltxt, resultSetType, resultSetConcurrency);
return _prestmt;
}

public ResultSet getRS(String sqltxt)
throws Exception, SQLException
{
Statement _stmt = getStmt();
ResultSet _rs = _stmt.executeQuery(sqltxt);
System.out.println("getRS(sqltxt)");
return _rs;
}


public ResultSet getRS(String sqltxt, int resultSetType, int resultSetConcurrency)
throws Exception, SQLException
{
Statement _stmt = getStmt(resultSetType, resultSetConcurrency);
ResultSet _rs = _stmt.executeQuery(sqltxt);
return _rs;
}

public ResultSet getRS(String sqltxt, Connection _conn)
throws Exception, SQLException
{
Statement _stmt = getStmt(_conn);
ResultSet _rs = _stmt.executeQuery(sqltxt);
System.out.println("getRS(sqltxt,_conn)");
return _rs;
}


public ResultSet getRS(String sqltxt, Connection _conn, int resultSetType, int resultSetConcurrency)
throws Exception, SQLException
{
Statement _stmt = getStmt(_conn, resultSetType, resultSetConcurrency);
ResultSet _rs = _stmt.executeQuery(sqltxt);
return _rs;
}

public ResultSet getRS(String sqltxt, Statement _stmt)
throws Exception, SQLException
{
ResultSet _rs = _stmt.executeQuery(sqltxt);
System.out.println("getRS(sqltxt,_stmt)");
return _rs;
}

public int updateDb(String sqltxt)
throws Exception, SQLException
{
Statement _stmt = getStmt();
int enum;
try
{
int num = _stmt.executeUpdate(sqltxt);
int i = num;
return i;
}
catch(SQLException ex)
{
enum = 0 - ex.getErrorCode();
}
int j = enum;
return j;
}

public int updateDb(String sqltxt, Statement _stmt)
throws Exception, SQLException
{
int enum;
try
{
int num = _stmt.executeUpdate(sqltxt);
int i = num;
return i;
}
catch(SQLException ex)
{
enum = 0 - ex.getErrorCode();
}
int j = enum;
return j;
}
}
...全文
396 点赞 收藏 13
写回复
13 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Web 开发
创建于2007-09-28

5.2w+

社区成员

34.1w+

社区内容

Java Web 开发
社区公告
暂无公告