Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与其返回整个表,我如何才能返回他们搜索的行?_Java_Mysql_Jsp_Search - Fatal编程技术网

Java 与其返回整个表,我如何才能返回他们搜索的行?

Java 与其返回整个表,我如何才能返回他们搜索的行?,java,mysql,jsp,search,Java,Mysql,Jsp,Search,新的编程,和这个网站。我当前的.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.*"%> <!DOCTYP

新的编程,和这个网站。我当前的.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成为用户搜索的名称。谢谢

您希望更改为PreparedStatement,以便它可以接受参数

PreparedStatement statement = c.prepareStatement("select * from videogamedetails where vidgamename = ?");
statement.setString(1, "nameOfVideoGame");
此外,还有两个一般提示:

  • 在JSP中使用EL/JSTL而不是Scriptlet是最佳实践
  • 最好的做法是不让Java代码进入JSP。在线搜索MVC,了解如何将两者分开

  • 我不确定您是如何将搜索参数p_Name传递给这个JSP的

    一种方法是将p_名称设置为请求属性,从该属性重定向到此页面,然后在此jsp上检索它

      String p_Name = (String) request.getAttribute("someVariable");
    
    使用prepared语句将值传递给查询

      PreparedStatement statement = c.prepareStatement("select * from videogamedetails where vidgamename = ?");
      statement.setString(1,  p_Name);
    
      rs = statement.executeQuery();
    

    p_Name
    的值应在请求参数中。此外,与其发送游戏名称,不如发送id。因为您正在学习,请正确操作并停止使用Scriptlet,这是非常不鼓励的,请参阅以获取解释。另外,学习MVC模式和分层应用程序也是一个很好的机会。OP的问题是如何在JSP中获得视频游戏的名称。错过了。所以应该是statement.setString(1,request.getParameter(“searchParamName”)。可能,可能不是。我们甚至不确定OP是否将此作为请求参数传递,或者只是从Servlet检索所有数据,或者他/她做了什么。我不确定如何让p_名称成为用户搜索的名称。你读到了吗?如果是,你的回答中哪里谈到在JSP中检索p_名称?