前几天用jsp连接数据库时可以正常连接数据库输出查询内容,前天忽然输出不出来了,我重启了电脑之后,又可以正常输出了,可今天忽然又不能输出查询内容
这是我编写的数据库通用类,链接数据库的通用类我就不贴上来了,已经确认过可以连接成功
package dbutil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conndb {
//执行select语句
//@param sql
//@return
//@throws Exception
public ResultSet executeQuery(String sql) throws Exception{
System.out.println("conn00");
ResultSet rs=null;
Connection con=null;
Statement st=null;
try {
con=conn.getConnection();
System.out.println("conn11");
st=con.createStatement();
System.out.println("conn12");
rs=st.executeQuery(sql);
} catch (SQLException e) {
throw e;
} finally{
DBclose.close(rs,st,con);
}
return rs;
}
//执行insert,update,delete语句
//@param sq;
//@throw Exception
public void executeUpdate(String sql) throws Exception{
Connection con=null;
Statement st=null;
try {
con=conn.getConnection();
st=con.createStatement();
st.executeUpdate(sql);
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("执行SQL语句出错:"+ex.getMessage());
} finally{
DBclose.close(st,con);
}
}
}
这是要执行的jsp语句,可以确定if(rs!=null){开始不执行的,控制台也没有报错
<body>
<%@page import="dbutil.Conndb" %>
<%@page import="dbutil.DBclose" %>
<%
request.setCharacterEncoding("utf8");
// String cla=request.getParameter("cla");
String cla="软件技术四班";
int id;
String s_name;
String t_name;
String sql=null;
Connection con=null;
Statement st=null;
ResultSet rs=null;
StringBuffer buffer = new StringBuffer();
try {
sql="select student.s_id,s_name,t_name from ts,student,team where student.s_id=ts.s_id and team.t_id=ts.t_id and ts.s_id in (select s_id from student where c_id in(select c_id from class where c_name='"+cla+"'))";
Conndb Conndb=new Conndb();
rs=Conndb.executeQuery(sql);
System.out.println("conn");
if(rs!=null){
buffer.append("<table width=100% height=15% border=2 cellspacing=0>");
buffer.append("<tr><td>学生id</td><td>学生姓名</td><td>加入社团</td></tr>");
while(rs.next()){
System.out.println("conn2");
id=rs.getInt("s_id");
s_name=rs.getString("s_name");
t_name=rs.getString("t_name");
buffer.append("<tr><td>"+id+"</td>");
buffer.append("<td>"+s_name+"</td>");
buffer.append("<td>"+t_name+"</td></tr>");
}
buffer.append("</table>");
}
} catch (Exception e) {
%>
<script type="text/javascript">
alert("出现问题,请重新输入");
</script>
<%
} finally{
DBclose.close(rs,st,con);
}
而另一个原来能正常运行的jsp页面报错"java.sql.SQLException: Operation not allowed after ResultSet closed"
String sql="select*from team where t_id='"+id+"'";
Conndb Conndb=new Conndb();
rs=Conndb.executeQuery(sql);
if(rs!=null){
while(rs.next()){
id=rs.getString("t_id");
name=rs.getString("t_name");
teacher=rs.getString("t_teacher");
time=rs.getString("t_time");
office=rs.getString("t_office");
}
}