我正在尝试从hsqldb
检索数据,并以表格的形式将其显示给用户。我正在使用while循环来查看是否有更多数据要显示,但jsp页面正在抛出编译器错误!请帮忙。
以下是我的jsp代码:
<%
Connection con = JDBCHelper.getConnection();
PreparedStatement ps_sel = null;
HttpSession s = request.getSession(false);
ResultSet rs = null;
String username = (String)s.getAttribute("username");
String string = (String)request.getAttribute("string");
try {
ps_sel = con.prepareStatement("select * from tasks where username = ? and (tname like ? or tasks like ?)");
ps_sel.setString(1, username);
ps_sel.setString(2, "%"+string+"%");
ps_sel.setString(3, "%"+string+"%");
ps_sel.execute();
rs = ps_sel.getResultSet();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = ps_sel.getResultSet();
while(rs.next())
{
%>
<tr>
<td><%= rs.getString(1)%></td>
<td><%= rs.getString(2)%></td>
<td><%= rs.getString(3)%></td>
<td><%= rs.getString(4)%></td>
<td><%= rs.getString(5)%></td>
<td><%= rs.getString(6)%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
答案 0 :(得分:0)
<%@ page import="java.sql.*" %>
<% Class.forName("org.hsqldb.jdbcDriver"); %>
<HTML>
<HEAD>
<TITLE>Fetching Data From a Database</TITLE>
</HEAD>
<BODY>
<H1>Fetching Data From a Database</H1>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>City</TH>
<TH>State</TH>
<TH>Country</TH>
</TR>
<%
Connection connection = DriverManager.getConnection( "Url", "YourName", "Password");
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
HttpSession s = request.getSession(false);
ResultSet rs = null;
String username = (String)s.getAttribute("username");
String string = (String)request.getAttribute("string");
try {
ps_sel = con.prepareStatement("select * from tasks where username = ? and (tname like ? or tasks like ?)");
ps_sel.setString(1, username);
ps_sel.setString(2, "%"+string+"%");
ps_sel.setString(3, "%"+string+"%");
ps_sel.execute();
rs = ps_sel.getResultSet();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = ps_sel.getResultSet();
while(rs.next())
{
%>
<TR>
<TD> <%= resultset.getString(1) %> </TD>
<TD> <%= resultset.getString(2) %> </TD>
<TD> <%= resultset.getString(3) %> </TD>
<TD> <%= resultset.getString(4) %> </TD>
<TD> <%= resultset.getString(5) %> </TD>
</TR>
<%
}
%>
</TABLE>
</BODY>
</HTML>
答案 1 :(得分:0)
jsp文件中的预处理语句在语法上是合法的。但避免使用它们。使用POJO编写java代码。可以将jstl用于相同的目的。如果你是初学者,那么在这里使用jstl是重要的学习方法。