我是一个初学者程序员,我已经为数据输入和页面显示所有条目建立了页面,但现在我想显示表内的特定行,哪些代码可以帮助我?
我的表列:全名-电子邮件-电话-教育我想通过电子邮件搜索,在一个页面中显示其他数据。
我在网上找到了这个代码:
<%@page import="java.sql.*"%>
<% Class.forName("com.mysql.jdbc.Driver");%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
public class Showit {
String URL = "jdbc:mysql://localhost/regdata";
String USERNAME = "root";
String PASSWORD = "admin";
Connection conn = null;
PreparedStatement selectRegister = null;
ResultSet resultSet = null;
public Showit() {
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
selectRegister = conn.prepareStatement(
"SELECT a.fullname, a.email,"
+ " FROM mainr a,"
+ "WHERE a.fullname = ?"
+ "AND a.email = ?");
} catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet getShowit(String fullname, String email) {
try {
selectRegister.setString(1, fullname);
selectRegister.setString(2, email);
resultSet = selectRegister.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return resultSet;
}
}
%>
<%
String fullname = new String();
String email = new String();
if (request.getParameter("fullname") != null) {
fullname = request.getParameter("fullname");
}
if (request.getParameter("email") != null) {
fullname = request.getParameter("email");
}
Showit showit = new Showit();
ResultSet showits = showit.getShowit(fullname, email);
%>
<table border="1">
<tbody>
<tr>
<td>Full Name</td>
<td>Email</td>
<td>Title</td>
</tr>
<% while (showits.next()) {%>
<tr>
<td><%= showits.getString("fullname")%></td>
<td><%= showits.getString("email")%></td>
<td><%= showits.getString("Phone")%></td>
</tr>
<% }%>
</tbody>
</table>
</body>
</html>
连接本页:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.Scanner" %>
<% Class.forName("com.mysql.jdbc.Driver");%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search</title>
</head>
<body>
<form name="search" action="display.jsp" method="POST">
<table border="0">
<tbody>
<tr>
<td>Full Name</td>
<td><input type="text" name="fullname" value="" size="50" /></td>
</tr>
<tr>
<td>E-Mail</td>
<td><input type="text" name="email" value="" size="50" /></td>
</tr>
</tbody>
</table>
<input type="reset" value="Reset" name="reset" />
<input type="submit" value="Submit" name="Submit" />
</form>
</body>
</html>
问题可能出在这一行:
fullname = request.getParameter("email");
请注意,您正在将email参数分配给fullname变量。
首先我看到的是查询是错误的。删除表别名"a"后面的,如下所示(这里是第2行):
"SELECT a.fullname, a.email,"
+ " FROM mainr a"
+ "WHERE a.fullname = ?"
+ "AND a.email = ?");
这里还有一个问题:
<td><%= showits.getString("Phone")%></td>
您没有在SELECT语句中包含Phone,因此它将不存在于ResultSet中。