数据库连接数量过多的问题,太蹊跷了,请各位高手帮忙看看看

zhhhhao 2004-11-19 11:41:37
这个是我的程序
<%
try{

//取当前系统时间
Calendar sttime=Calendar.getInstance();
int year=0;
int month=0;
int day=0;
year=sttime.get(Calendar.YEAR);
month=sttime.get(Calendar.MONTH)+1;
day=sttime.get(Calendar.DAY_OF_MONTH);
String str_year,str_month,str_day,str_time;
str_year=Integer.toString(year);
str_month=Integer.toString(month);
str_day=Integer.toString(day);
str_time=str_year+"-"+str_month+"-"+str_day;


int row=0;
java.sql.Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:archive");

Statement stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
String sql="select * from d_baseinfo where workdate<='1900-1-1' or workdate>'"+str_time+"' or dutydate<='1900-1-1' or dutydate>'"+str_time+"' or offworkdate<='1900-1-1' or offworkdate>'"+str_time+"' or Armydate<='1900-1-1' or Armydate>'"+str_time+"' and totalunitname='"+dw_zong_edit+"'";

//String sql="select name,personid from d_baseinfo group by name,personid having count(unitname)>1";
//String sql="select * from d_baseinfo ";
//String sql="select yd_name from yd_lb group by yd_name having count(*)>1";
//out.println(sql);

ResultSet rs=stmt.executeQuery(sql);

rs.last();
row=rs.getRow();
rs.first();
//out.println(row_rc)
//out.println(row);
if (row>0)
{
while(!rs.isAfterLast())
{
String dw=rs.getString("totalunitname");
String dw_xia=rs.getString("unitname");
String name=rs.getString("name");
String personid=rs.getString("personid");
String wkdate=rs.getString("workdate");
String ddate=rs.getString("dutydate");
String offdate=rs.getString("offworkdate");
String adate=rs.getString("Armydate");
%>

<tr>
<td width="188"><p align="center"><font size="2"><%=dw%> <input type=hidden name=dw value="<%=dw%>"></td>
<td width="119"><p align="center"><font size="2"><%=dw_xia%><input type=hidden name=dw_xia value="<%=dw_xia%>"> </td>
<td width="96"><p align="center"><font size="2"><%=name%><input type=hidden name=name value="<%=name%>"></td>
<td width="165"><p align="center"><font size="2"><%=personid%> </font><input type=hidden name=personid value="<%=personid%>"></td>
<td width="114"><p align="center"><font size="2"><%=wkdate%><input type=hidden name=workdate value="<%=wkdate%>"></td>
<td width="120"><p align="center"><font size="2"><%=ddate%> <input type=hidden name=dutydate value="<%=ddate%>"></td>
<td width="106"><p align="center"><font size="2"><%=offdate%><input type=hidden name=offworkdate value="<%=offdate%>"></td>
<td width="108"><p align="center"><font size="2"><%=adate%> <input type=hidden name=armydate value="<%=adate%>"></td>
</tr>
<%

rs.next();
}
}

}catch(Exception e){
out.println("数据库连接失败!");
out.println(e.toString());
}finally{
//rs.close();
stmt.close();
conn.close();

}
这个是我的报错信息,怎么会提示,无效的stmt和conn呢
D:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\police\yd\sh_005ftime_jsp.java:433: cannot resolve symbol
symbol : variable stmt
location: class org.apache.jsp.police.yd.sh_005ftime_jsp
stmt.close();
^



An error occurred at line: 117 in the jsp file: /police/yd/sh_time.jsp

Generated servlet error:
D:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\police\yd\sh_005ftime_jsp.java:434: cannot resolve symbol
symbol : variable conn
location: class org.apache.jsp.police.yd.sh_005ftime_jsp
conn.close();
^
2 errors

...全文
213 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jFresH_MaN 2004-11-20
  • 打赏
  • 举报
回复
就是写一个数据库访问的类
然后写一个close()方法
在这个方法里面关闭,ResultSet,Statement,Connection
zhhhhao 2004-11-20
  • 打赏
  • 举报
回复
请具体一点
jFresH_MaN 2004-11-19
  • 打赏
  • 举报
回复
这个问题很明显嘛
你的conn和stmt是在try里面创建的,在finally里面是不可见的
所以,你要把他们在try之前创建
大家都糊涂了
呵呵
szpqq 2004-11-19
  • 打赏
  • 举报
回复
声明变量在最前面,在try{上面
java.sql.Connection conn = null;//
java.sql.Statemenet stmt = null;
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
d
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
不是,错误提示是无效的conn和stmt,java.sql*倒入了,还有,我没有加finally{
//rs.close();
stmt.close();
conn.close();

}这个的时候程序是可以运行的
fmzbj 2004-11-19
  • 打赏
  • 举报
回复
你有没有导入:java.sql.*;再就是你这里错了:
conn=DriverManager.getConnection("jdbc:odbc:archive");

应该为:Connection conn=DriverManager.getConnection("jdbc:odbc:archive");

或者:java.sql.Connection conn = null;//要记得初始化
conn=DriverManager.getConnection("jdbc:odbc:archive");

你conn有错,stmt当然会报错啦,试试吧
gks_cn 2004-11-19
  • 打赏
  • 举报
回复
你应该把你的数据库访问模块封装一下了。那样显示的调用关闭连接的方式。太容易漏掉了。
2000个也面中要是有一个也面没有关闭。这个也面同时刷几百下。有连接池也没有用的。
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
d
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
并没有解决,可能还会提示连接过多,虽然我已经在try中关闭了conn
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
问题是,我就是这样报错的,我又不能用连接池,原因不要问,不能改很多东西
wangwei8117 2004-11-19
  • 打赏
  • 举报
回复
应该解决了吧,我来晚了点!
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
那我这样可以吧try{
java.sql.Connection conn;
.
.
.
.
.
conn.close

}
bp69 2004-11-19
  • 打赏
  • 举报
回复
建议还是用连接池,一个物理conn,可以实现200多个会话,这样基本不会出现连接过多的情况
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
d
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
我的客户就说,有连接过多的错误提示,现在我就是想解决这个问题,但是我所有在try 中建立了conn ,同时在try中也关闭了conn.close();,为什么还有错误呢,还有,我的java.sql.Connetion conn是在try中申明的,请问不用连接池有什么办法,用代码实现
jFresH_MaN 2004-11-19
  • 打赏
  • 举报
回复
要在finally里面关闭,这样即使出现异常也能关闭的
zhhhhao 2004-11-19
  • 打赏
  • 举报
回复
以下是csdn里面的人发给我的帖子
《--应该在没次用try{}住conn.open().然后在finally{}里面用conn.close()
这样应该就可以每次打开连接,每次都关闭
——》
他要在try里面打开conn,在finally中关闭conn,我是在try中关闭的,到底是怎么回事,请各位帮忙解答一下

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧