回复:(polestar707)[求助]请教关于数据库连接的问题...
是啊,我现在在用JSP+JAVABEAN+ACCESS给本公司内部开发一套HR管理系统,也出现了类似的问题,并且我发现,在查询记录时才会出现上述问题,在修改记录时不会(此时可以关闭connection):
1、查询记录时出错:
public String[][] pubGetResultSet(String sql) throws Exception{
String sSql=null;
String sTmp;
String[][] sArrRtn=null;
int iCol=0,i=0,j=0;
boolean bFlg=false;
Connection con=null;
Statement st=null;
ResultSet rs=null;
ResultSetMetaData rsmd=null;
sSql =sql;
try{
con=JdbcInf.pubGetConnection();
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery(sSql);
rsmd=rs.getMetaData();
iCol=rsmd.getColumnCount();
//System.err.println(con.getMetaData().getSystemFunctions());
bFlg=rs.last();
if(bFlg) i=rs.getRow();
rs.beforeFirst();
sArrRtn=new String[i+1][iCol];
// 取得列名
for(i=0;i<iCol;i++)
{
sTmp=rsmd.getColumnName(i+1).trim();
sArrRtn[0][i]=sTmp;
}
// 取得每行记录的值
j=0;
while(rs.next()){
j=j+1;
for(i=0;i<iCol;i++)
{
sTmp=String.valueOf(rs.getObject(i+1)).trim();
if(sTmp=="null") sTmp = "";
sArrRtn[j][i]=sTmp;
}
}
return sArrRtn;
}
catch(SQLException se){
System.err.println("异常: " + se);
System.err.println("sql: " + sql);
throw se;
}
finally
{
try{
if (rs!=null) rs.close();
if (st!=null) st.close();
// 在此处出错!!!
if (con!=null) con.close();
}catch(SQLException se){throw se;}
}
}
2、修改记录时正常:
public boolean modifyStuffCertificate(long id, String certificate_no, String certificate_name, String issued_date, String issued_org, long available_time_limit, boolean if_need_annualy_check){
Connection conn = null;
Statement stmt = null;
String sql = "";
try {
info = "修改员工证件资料信息->连接数据库";
conn = JdbcInf.pubGetConnection();
stmt = conn.createStatement();
info = "修改员工证件资料信息->修改";
sql = "UPDATE T_STUFF_CERTIFICATE SET CERTIFICATE_NO = '" + certificate_no + "', CERTIFICATE_NAME = '" + certificate_name +
"', ISSUED_DATE = '" + issued_date + "', ISSUED_ORG = '" + issued_org + "', AVAILABLE_TIME_LIMIT = " + available_time_limit + ", IF_NEED_ANNUALY_CHECK = " + if_need_annualy_check + " WHERE ID = " + id;
stmt.execute(sql);
conn.commit();
return true;
}catch(Exception e){
System.err.println("sql = " + sql);
System.err.println(info + ":" + e);
return false;
}finally{
if(stmt!=null){
try{
stmt.close();
}catch(Exception se){
}
}
if(conn != null){
try{
// 可以正常关闭哦!!!
conn.close();
}catch(Exception se){
}
}
}
}