新编程和本网站。我当前的.jsp是
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page import="java.sql.*"
import="action.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Here is your videogame!</title>
</head>
<BODY>
<H1>The Videogame Database Table </H1>
<%
Connection c = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/videogamesdb", "root", "password");
//instantiating a SQL statement
Statement statement = c.createStatement();
String sql = "SELECT * FROM videogamedetails";
ResultSet resultset =
statement.executeQuery(sql) ;
%>
<TABLE BORDER="1">
<TR>
<TH>Name</TH>
<TH>Genre</TH>
<TH>Developer</TH>
<TH>Rating</TH>
</TR>
<% while(resultset.next()) {%>
<TR>
<TD> <%= resultset.getString("vidgameName") %></TD>
<TD> <%= resultset.getString("vidgameGenre") %></TD>
<TD> <%= resultset.getString("vidgameDev") %></TD>
<TD> <%= resultset.getInt("vidgameRating") %></TD>
</TR>
<% } %>
</TABLE>
</BODY>
</HTML>
我很确定我需要使用“SELECT * FROM videogamedetails WHERE vidgameName = p_Name”,但我不确定如何让p_Name成为用户搜索的内容。谢谢!
答案 0 :(得分:0)
您想要更改为PreparedStatement,以便它可以使用参数。
PreparedStatement statement = c.prepareStatement("select * from videogamedetails where vidgamename = ?");
statement.setString(1, "nameOfVideoGame");
另外,有两个一般提示:
答案 1 :(得分:0)
我不确定您是如何将搜索参数p_Name传递给此JSP的。
一种方法是将p_Name设置为请求属性,从重定向到此页面,然后在此jsp上检索它。
String p_Name = (String) request.getAttribute("someVariable");
使用预准备语句将值传递给您的查询。
PreparedStatement statement = c.prepareStatement("select * from videogamedetails where vidgamename = ?");
statement.setString(1, p_Name);
rs = statement.executeQuery();