(简单)servlet连接数据库的问题,解决必送分!

lucianliu 2003-06-16 11:16:45
我在servlet中连接数据库读取数据,执行完毕,我关掉该页面,再重新来一遍的时候就会出现提示:SQLException caught: 关闭的连接
怎么回事呢?
...全文
52 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
withwind 2003-06-16
  • 打赏
  • 举报
回复
不要关闭连接
Philzzf 2003-06-16
  • 打赏
  • 举报
回复
把代码贴出来
lucianliu 2003-06-16
  • 打赏
  • 举报
回复
这个servlet需要2次连接数据库查询数据,其中一次是在一个自定义函数中,那么我是否需要
2次定义con马?我觉得定义一个public就可以了吧
lucianliu 2003-06-16
  • 打赏
  • 举报
回复
就是在servlet中processRequest(HttpServletRequest request, HttpServletResponse response)建立的
Philzzf 2003-06-16
  • 打赏
  • 举报
回复
可能是你在用完数据库连接的时候忘记关闭了
记得用con.close()来关闭数据库
rong_xing 2003-06-16
  • 打赏
  • 举报
回复
应该是说你的数据库连接已经被关闭了
你这个数据库连接是在哪里创建的?
lucianliu 2003-06-16
  • 打赏
  • 举报
回复
i think i have gotton the reason by myself.
i have one class whose function is getting connection with oracle in which there is a
Connection object.i must creat another Connection object to receive the Connection
return value when i invoke the class.so there r 2 connection object.i have do to do with
these two Connection object when i need data from db.there must be some problem.
once more i think feibaook(飞豹) and other netpla's feedback.thanks!
feibaook 2003-06-16
  • 打赏
  • 举报
回复
建议将你的数据库操作放到单独的方法中,在每次取数据时都调用,每次用完后记得关闭。
你现在的问题是在第一次操作之后,关闭掉了数据库连接,在第二次操作时没有连接数据库,所以抛出了上面的异常。
另外,processRequest我没试过,但在init方法中所有的操作,在每个会话周期中只调用一次。比如说,你把连接数据库的操作放到了init方法里,二关闭数据库连接的操作在别的方法里,那么在一个会话周期中多次调用同一个servlet的话,就会出现你的问题。
public class MyServlet extends HttpServlet {
Connection conn = null;
public void init(...) {
conn = ...;
}

public void yourFuction() {
...
conn.close();
}
}
向上面的用法是要报你的错误的!建议改为
public void yourFuction() {
Connection conn = ...;
...
conn.close();
}
qizi168 2003-06-16
  • 打赏
  • 举报
回复
你是不是线关闭连结了,又引用这个连结进行了数据库操作?要不就是载连结时候顺手关闭了?
wangyanqiu 2003-06-16
  • 打赏
  • 举报
回复
错误信息!
omphalos 2003-06-16
  • 打赏
  • 举报
回复
是不是应该关闭db

81,092

社区成员

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

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