请教一个连接数据库javaBean的问题

simplemanNO1 2004-09-18 11:46:12
代码如下:
***************************************************************
package note;
import java.sql.*;

public class Jdbc_conn{
String sDBDriver="com.mysql.jdbc.Driver";
String sConnStr="jdbc:mysql://localhost:3306/sample_db?user=root&password=root";
Connection conn=null;
ResultSet rs=null;

public Jdbc_conn(){
try
{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e)
{

System.out.println("Jdbc_conn():"+e.getMessage());
}
}

public void executeUpdate(String sql)throws Exception{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
stmt.close();
conn.close();

}
catch(SQLException ex){
System.out.println("sql.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)throws Exception{
rs=null;
try{
sql=new String(sql.getBytes("GBK"),"ISO8859_1");
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
stmt.close();
conn.close();

}
catch(SQLException ex){
System.out.println("sql.executeQuery:"+ex.getMessage());
}
return rs;
}


}
********************************
用以上的代码会出现如下的错误:
***************************************
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Operation not allowed after ResultSet closed
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.index_jsp._jspService(index_jsp.java:180)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.sql.SQLException: Operation not allowed after ResultSet closed
com.mysql.jdbc.ResultSet.checkClosed(ResultSet.java:3601)
com.mysql.jdbc.ResultSet.next(ResultSet.java:2478)
org.apache.jsp.index_jsp._jspService(index_jsp.java:80)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


note The full stack trace of the root cause is available in the Tomcat logs.

****************

但是如果我去掉两个方法中的stmt.close();conn.close();就可以正常连接到数据库了,这是怎么回事啊,我觉得stmt.close();conn.close();写在方法后面没错啊,请高手指点!

...全文
76 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
gzkevinyang 2004-10-14
或者:
finally{
if(stmt!=null)
stmt=null;
if(con!=null)
con.close();
}
回复
gzkevinyang 2004-10-14
try{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);

conn.close();
}catch(SQLException ex){
System.out.println("sql.executeUpdate:"+ex.getMessage());
}
}finally{
if(stmt!=null)
stmt=null;
if(con!=null)
con=null;
}
回复
simplemanNO1 2004-09-19
是不是怪我没给分啊,回答后给100分成不?
回复
simplemanNO1 2004-09-19
为什么没有人理我啊?
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-09-18 11:46
社区公告
暂无公告