在JSP中使用多个ResultSet

时间:2018-03-21 08:48:05

标签: java mysql jsp servlets glassfish-4

我对JSP很新。 我试图选择2个sql语句来生成2个单独的表。我可以成功选择1个表,但是当我尝试2个表时,我无法运行它。 我做的是。

设置我的连接:

<%
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String dbName = "supr";
String userId = "root";
String password = "secret";

try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
ResultSet resultSet2 = null;
%>

连接并执行查询

<%
       try {
       connection = DriverManager.getConnection(
       connectionUrl + dbName, userId, password);
       statement = connection.createStatement();
       String sql = "SELECT con.container_id, con.con_location, con.con_status, concap.capacity FROM container con INNER JOIN con_capacity concap ON con.container_id = concap.container_id";

       String sql2 = "SELECT p.pipe_id, p.pipe_location, pd.PipeDis_date FROM pipe p JOIN pipe_disinfect pd ON p.pipe_id = pd.pipe_id ";
       resultSet = statement.executeQuery(sql);
       resultSet2 = statement.executeQuery(sql2);
       %>

第一张桌子很有效。

<table class="table table-hover">
          <thead>
           <th>Container ID</th>
           <th>Location</th>
           <th>Status</th>
           <th>Capacity</th>
           <th></th>
          </thead>
          <tbody>
           <%
           while (resultSet.next()) { %>
           <tr>
            <td><%=resultSet.getString("Container_ID")%></td>
            <td><%=resultSet.getString("Con_Location")%></td>
            <td><%=resultSet.getString("Con_Status")%></td>
            <td><%=resultSet.getString("Capacity")%></td>
            <td><button class="btn btn-primary">Schedule</button></td>
           </tr>
          </tbody>
          <%
         }

        } catch (Exception e) {
        e.printStackTrace();
       }
       %>                                          
      </table>

但是当我尝试与下面的第二个表组合时,我无法让它运行。

<table class="table table-hover">
       <thead>
        <th>Pipe ID</th>
        <th>Location</th>
        <th>Last Disinfection</th>
        <th></th>
       </thead>
       <tbody>
        <%
        while (resultSet2.next()) { %>
        <tr>
         <td><%=resultSet2.getString("Pipe_ID")%></td>
         <td><%=resultSet2.getString("Pipe_Location")%></td>
         <td><%=resultSet2.getString("Pipe_LastDisinfect")%></td>
         <td><button class="btn btn-primary">Schedule</button></td>
        </tr>
       </tbody>
       <%  /**I'm getting an error on this line on eclipse****/
      }

     } catch (Exception e) {
     e.printStackTrace();
    }
    %>                                          
   </table>

我从GlassFish得到了内部服务器错误

1 个答案:

答案 0 :(得分:0)

连接,语句和结果集必须为close() d。 因此,重用变量是行不通的。

Class.forName不再需要加载数据库供应商特定库jar。

<%
    String sql = "SELECT con.container_id, con.con_location, con.con_status, "
        + " concap.capacity "
        + "FROM container con "
        + "INNER JOIN con_capacity concap ON con.container_id = concap.container_id";
    try (Connection connection = DriverManager.getConnection(
            connectionUrl + dbName, userId, password);
        PreparedStatement statement = connection.prepareStatement(sql)) {
        try (ResultSet resultSet = statement.executeQuery()) {
%>
...
<%
        }
    }
%>

使用上面的try-with-resources语法,即使返回或抛出异常,也会自动完成关闭。

此变量用法也无需考虑statement2等不同的名称。