结果集的所有行未显示在 JSP 页 table 使用 JDBC postgresql
All rows of a Resultset NOT being displayed in JSP page table using JDBC postgresql
我是 jsp 的新手。这是我正在尝试做的事情:
-> 从用户那里输入关键字(来自 jsp 表单)
-> 在任何列中包含给定关键字的 postgres 关系数据库中搜索来自特定关系的元组。
我为此使用 JDBC。我以为一切都很好,直到我发现没有显示某些具有给定关键字的元组。
关于数据库:
数据库名称是 'university'。我的数据库 'course' 中有一个关系具有参数 (course_id, title, dept_name, credits)
这是 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>
我觉得一切都很好,但是 table 没有显示所有有效的元组。说真的,我需要你们的帮助。 (抱歉我的英语不好)。
您正在使用 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。
<%
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>
我是 jsp 的新手。这是我正在尝试做的事情: -> 从用户那里输入关键字(来自 jsp 表单) -> 在任何列中包含给定关键字的 postgres 关系数据库中搜索来自特定关系的元组。
我为此使用 JDBC。我以为一切都很好,直到我发现没有显示某些具有给定关键字的元组。
关于数据库: 数据库名称是 'university'。我的数据库 'course' 中有一个关系具有参数 (course_id, title, dept_name, credits)
这是 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>
我觉得一切都很好,但是 table 没有显示所有有效的元组。说真的,我需要你们的帮助。 (抱歉我的英语不好)。
您正在使用 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。
<%
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>