【问题标题】:"JSP First Connection cause second connection not running when it had ended"“JSP 第一个连接导致第二个连接在结束时没有运行”
【发布时间】:2019-10-05 08:09:53
【问题描述】:

我正在尝试从两个不同的表(流派和游戏)中获取两个数据(流派 ID 和游戏 ID)并将它们插入另一个表(游戏流派)。但是,即使我已经创建了另一个与数据库的新连接,它也会在成功插入 GenreID 后关闭与数据库的连接。

我尝试创建连接1 和连接2 到同一个数据库。 Connection1 用于插入 GenreID,connection2 用于插入 GameID

<%@ page import="java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
String gametitle = request.getParameter("gametitle");
String [] checkbox1 = request.getParameterValues("checkbox");

try {
   Class.forName("com.mysql.cj.jdbc.Driver");
   String connURL ="jdbc:mysql://localhost/assignment?user=root&password=root&serverTimezone=UTC"; 

   Connection conn = DriverManager.getConnection(connURL);
   Connection conn2 = DriverManager.getConnection(connURL);

   Statement stmt = conn.createStatement();   
   if (checkbox1!= null){
      for(String s: checkbox1){
       String sqlStr2 = "Select * FROM genre WHERE GenreName='" + s + "'";  
       ResultSet rs = stmt.executeQuery(sqlStr2);
       while(rs.next()){
          String genreid = rs.getString("GenreID");
          String sqlStr3 = "INSERT INTO games_genre(GenreID) VALUES ('" + genreid + "')";
          int j = stmt.executeUpdate(sqlStr3);  
          if (j>0) {
           out.println("Adding GenreID Successfully!");}
       }                
      }
   }
   conn.close();

   Statement stmt2 = conn2.createStatement();
   String sqlStr4 = "Select * FROM games WHERE GameTitle='" + gametitle +"'";
   ResultSet rs2 = stmt2.executeQuery(sqlStr4);
   if(rs2.next()){
      String gameid = rs2.getString("GameID");
      String sqlStr5 = "INSERT INTO games_genre(GameID) VALUES ('" + gameid + "')";
      int k = stmt2.executeUpdate(sqlStr5); 
      if (k>0) {
     out.println("Adding GameID Successfully!");            
      }
   }

   conn2.close();
} catch (Exception e) {
   out.println("Error :" + e);
}

添加游戏成功!添加 GenreID 成功!错误:java.sql.SQLException:ResultSet 关闭后不允许操作

【问题讨论】:

    标签: mysql eclipse jsp


    【解决方案1】:

    我不明白您为什么需要创建两个 Connection ,因为您需要访问相同的 database 。所以,只需创建多个Statement 来执行多个查询,如下所示:

        Statement stmt=null;
        Statement stmt2=null;
           try {
                   Class.forName("com.mysql.cj.jdbc.Driver");
                   String connURL ="jdbc:mysql://localhost/assignment?user=root&password=root&serverTimezone=UTC"; 
                   Connection conn = DriverManager.getConnection(connURL);
                     stmt = conn.createStatement();   
                   if (checkbox1!= null){
                    ....
                   }
                <!--using same conn object -->
                stmt2 = conn.createStatement();
                   String sqlStr4 = "Select * FROM games WHERE GameTitle='" + gametitle +"'";
               ResultSet rs2 = stmt2.executeQuery(sqlStr4);
               if(rs2.next()){
                 ...
               }
          <!--finally close connection-->
             conn.close();  
    
         } catch (Exception e) {
       out.println("Error :" + e);
    }
    

    注意:还可以尝试使用 PreparedStatement 来防止 Sql Injection 将值连接到查询字符串中是不安全的。 p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      相关资源
      最近更新 更多