【问题标题】:All rows of a Resultset NOT being displayed in JSP page table using JDBC postgresql结果集的所有行未使用 JDBC postgresql 显示在 JSP 页表中
【发布时间】:2015-04-18 21:09:31
【问题描述】:

我是 jsp 的新手。这是我正在尝试做的事情: ->从用户那里获取关键字输入(来自jsp表单) ->在任何列中包含给定关键字的postgres关系数据库中从特定关系中搜索元组。

我为此使用 JDBC。我认为一切都很好,直到我发现一些具有给定关键字的元组没有被显示。

关于数据库: 数据库名称是“大学”。我的数据库“课程”中有一个具有参数(课程 ID、标题、部门名称、学分)的关系

这是我将从用户那里获取输入关键字的 jsp 页面的代码:

      <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>search courses</title>
      </head>
       <body>
      <h1>Search for courses</h1>
      <FORM ACTION="coursesearch.jsp" METHOD="POST">
        Type the keyword to search in course
        <br> <input type="text" name="keyword"   />
         <br> <INPUT TYPE="SUBMIT" value="Submit">
      </FORM>
    </body>
   </html>

这里是“coursesearch.jsp”的代码,它显示来自给定关键字的课程行:

<%@ page import="java.sql.*" %>
<% Class.forName("org.postgresql.Driver"); %>
<html>
    <head>
        <title>Question2</title>
    </head>
    <body>
        <h1>Courses Containing the given keyword : </h1>
        <%
            Connection connection = DriverManager.getConnection(
                    "jdbc:postgresql://localhost:5432/university","postgres","password");

            Statement statement1 = connection.createStatement();

            String keyword = request.getParameter("keyword");
            String sql = "select course_id,title from course where LOWER(title)  LIKE LOWER(?)";
          PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "%" + keyword + "%");
            ResultSet resultset1 = preparedStatement.executeQuery();
        if(!resultset1.next()){
            out.println("Sorry no course found having given keyword");
        }
        else {

        %>
        <p>
            Course IDs containing the given keyword 
        </p>
        <table border="1">

                <tr>
                    <th>Course ID</th>
                    <th>Course title </th>

                    <% while(resultset1.next()){ %>

                    <tr>
                       <td> <%=resultset1.getString(1)%> </td>
                         <td> <%=resultset1.getString(2)%> </td>
                    </tr>
                <% 
                resultset1.next();
                    } %>
            </table>
            <br>
              <% } 
            connection.close();
              %>
        </body>
     </html>

对我来说一切都很好,但是表格没有显示所有有效的元组。说真的,我需要你们的帮助。 (对不起我的英语不好)。

【问题讨论】:

    标签: java jsp jdbc


    【解决方案1】:

    您正在使用来自ResultSet 的行而没有注意到。

    这里:

    if(!resultset1.next()){
    //here you move from row 0 to row 1
    

    那么这里:

    while(resultset1.next()){
    

    最后,在这里:

    resultset1.next();
    

    基本上,您的代码如下所示:

    if(!resultset1.next()) {
        //advanced to the first row, but not consumed
        while(resultset1.next()){
            //advanced to the following row
            //consumed this one...
    
            resultset1.next();
            //yet again advancing to the next row
            //and not consuming it
        }
    }
    

    相关:ResultSet.getString(1) throws java.sql.SQLException: Invalid operation at current cursor position

    最后但同样重要的是,帮自己一个忙,avoid using scriptlets

    【讨论】:

      【解决方案2】:
          <%
      
              try{
              ResultSet rs = Database.getData("select * from event");
              while(rs.next()){
          %>      
      
      <tr>
          <td><%= rs.getString("id")  %></td>
      
      
          <td><%= rs.getString("time") %></td>
      
          <td><%= rs.getString("event") %></td>
      
          <td><a href="#" class="btn btn-xs btn-warning"><span class="glyphicon glyphicon-trash"></span> Remove</a></td>
      
      
      </tr>
      

      【讨论】:

      • 只是一些代码不是一个好的答案,你能解释一下为什么这样可以解决问题吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 2013-06-18
      • 1970-01-01
      • 2021-01-12
      • 2011-05-14
      • 1970-01-01
      • 2015-01-07
      相关资源
      最近更新 更多