如何在JSP文件中运行两个查询。以下是我的代码

时间:2014-05-24 19:27:00

标签: java sql jsp

我想执行以下代码,其中包含两个要在其中执行的SQL查询。我一直得到SQL异常。代码在这里。提前谢谢。

     <table border="1" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <th>FileName</th>
            <th>Ip</th>
            <th>Date</th>
            <th>Download</th>
        </tr>
        <%

            Connection con = DbConnector.getConnection();
            PreparedStatement pstm = null;
            String sql = "select * from transaction where     status='Success'";
            pstm = con.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();

            while (rs.next()) {%>
        <tr>
            <td><%=rs.getString(2)%></td>
            <td><%=rs.getString(4)%></td>
            <td><%=rs.getString(5)%></td>
            <% 
            PreparedStatement pstm1 = null;
            String sql2 = "select * from mykeys where FileID = '"+ rs.getString(7).toString() +"'";
            pstm1 = con.prepareStatement(sql2);
            ResultSet rs1 = pstm1.executeQuery();

                                                      if(rs1.getString(2).toString() != null &&      rs1.getString(3).toString() != null){%>
            <td><a href="FileDownload?ip=    <%=rs.getString(4)%>&filename=<%=rs.getString(2)%>">Download</a></td>
            <%}else{%>
            <td>Cannot Download</td>
            <%}%>
       </tr>

        <%}%>
</table> 

1 个答案:

答案 0 :(得分:3)

问题在于线下。最有可能的错误是你忘记在获取数据之前打电话给rs1.next()

ResultSet rs1 = pstm1.executeQuery();
if(rs1.getString(2).toString() != null &&  rs1.getString(3).toString() != null)

应该是

ResultSet rs1 = pstm1.executeQuery();
if(rs1.next() && rs1.getString(2).toString() != null && rs1.getString(3).toString() != null)

请查看JDBC Basics,了解JDBC API的基础知识。


注意:

JSP JSTL

上查找示例代码