错误:连接关闭后不允许任何操作

时间:2015-10-21 07:37:43

标签: java mysql jsp jdbc

我知道在JSP中放置SQL代码或任何与数据库相关的代码是个坏主意。但我想尝试这种方式,请帮助。 在下面的retrieve.jsp代码中,我必须输入empid和datefrom和dateto然后它将在dispaly.jsp页面上显示结果作为表格,其中包含员工的所有详细信息和总工作时间以及加班时间。

retrieve.jsp

  $("#leaveCode").change(function(){
$("#leaveCode1").append("<option value=" + $("#leaveCode").val() + ">" + $("#leaveCode option:selected").text() + "</option>")
});

display.jsp

%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="com.eis.bean.Provider"%>
<%@page import="com.eis.bean.ConnectionProvider"%>
<%@page import="java.sql.*" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h4>Enter Employee ID and the dates</h4>
    <form name="retrieve form" action="display1.jsp" method="POST">
        <table border="0">

            <tbody>
                <tr>
                    <td>Employee ID</td>
                    <td><input type="text" name="Emp_id" required="required"/></td>
                </tr>
                <tr>
                    <td>From Date:</td>
                    <td><input type="date" name="From" value="yyyy/MM/dd" required="required"/></td>
                </tr>
                <tr>
                    <td>To Date:</td>
                    <td><input type="date" name="To" value="yyyy/MM/dd" required="required"/></td>
                </tr>
            </tbody>
        </table>
        <input type="reset" value="Clear" name="clear" />
        <input type="submit" value="Submit" name="submit" />
    </form>
</body>
</html>

ConnectionProvider.class

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="com.eis.bean.Provider"%>
<%@page import="com.eis.bean.ConnectionProvider"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>

<%
String empid = request.getParameter("Emp_id");
String from = request.getParameter("From");
String to = request.getParameter("To");
Connection conn= null;
PreparedStatement ps = null; 
ResultSet rs = null; 

conn = ConnectionProvider.getConn(); 
try {

    ps = conn.prepareStatement("SELECT * FROM timsheetdb.logintable WHERE Emp_id=? and LoginDate BETWEEN ? AND ?; ");
    ps.setString(1, empid); 
    ps.setString(2, from);
    ps.setString(3, to);

    out.print("<table border=1>");  
    out.print("<caption>TimeSheet </caption>");  
    rs = ps.executeQuery();  
    ResultSetMetaData rsmd = rs.getMetaData(); 
    int total=rsmd.getColumnCount();  
     if (rs.next()) {
 %><jsp:include page="retrieve.jsp"/>
<%

    out.print("<center><h1>Timsheet</h1></center>");
    out.print("<b>Employee ID</b> " + empid + "");
    out.print("<br>" + "<b>From Date:</b> " + from);
    out.print("<br>" + "<b>To Date:</b> " + to);
    out.print("<tr>");  
        for(int i=1;i<=total;i++)  
        {  
        out.print("<th>"+rsmd.getColumnName(i)+"</th>");  
        }  
        out.print("</tr>");
 while(rs.next())  
        {  
        out.print("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td>"+rs.getString(5)+"</td><td>"+rs.getString(6)+"</td><td>"+rs.getString(7)+"</td><td>"+rs.getString(8)+"</td><td>"+rs.getString(9)+"</td><td>"+rs.getString(10)+"</td></tr>");  

        }  
        out.print("</table>");  
} 
else
     { 
out.println("Please check the Employee ID and Dates "); 
%>
<jsp:include page="retrieve.jsp"/>
<%}
} catch (Exception ex) {
    System.out.println(ex);
}   
finally {  
         if (conn != null) {  
            try {  
                conn.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        } 

        if (ps != null) {  
            try {  
                ps.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
        if (rs != null) {  
            try {  
                rs.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  

    }  
%>

以下是错误:

import java.sql.*; 

public class ConnectionProvider {  
  private static Connection conn=null;  
  static{  
    try{  
      Class.forName("com.mysql.jdbc.Driver");
      conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/timsheetdb","root","lion");  
    }catch(Exception e){}  
  }  

  public static Connection getConn(){  
    return conn;  
  }  

}

1 个答案:

答案 0 :(得分:2)

您的联系是使用ConnectionProvider.getConn();

获得的

该函数必须确保返回的连接仍处于打开状态,否则返回新的连接。

我想目前它总是返回只创建一次的连接的同一个实例。

编辑:将您的ConnectionProvider更改为:

public class ConnectionProvider {  
  public static Connection getConn(){  
    try {  
      Class.forName("com.mysql.jdbc.Driver");
      return DriverManager.getConnection("jdbc:mysql://localhost:3306/timsheetdb","root","lion");  
    } catch(Exception e){}  

    return null;  
  }  
}

这不是很好的代码,我个人不会那样使用它,但它应该可以解决你的问题。现在每次都会创建一个与数据库的新连接。