【发布时间】: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>
对我来说一切都很好,但是表格没有显示所有有效的元组。说真的,我需要你们的帮助。 (对不起我的英语不好)。
【问题讨论】: