【问题标题】:searching data from database on dynamic web page在动态网页上从数据库中搜索数据
【发布时间】:2017-05-16 00:12:36
【问题描述】:

我的网站代码有问题。当用户可以在 jsp 页面上搜索活动时,我正在编写一个网站。数据库根据活动的类型、城市、位置和日期包括一些活动。用户将填写此信息并按提交按钮。将打开名为 search.jsp 的新 jsp 页面。用户只能通过填写一个字段进行搜索,例如只需填写类型并写入“音乐”。他也可以通过填写 2 或 3 或所有字段进行搜索,例如填写“音乐”、“伊斯坦布尔”和“本周”。按下提交按钮后。数据将显示在搜索页面上。但我不能这样做。我能怎么做 ? (PS:sql中的表名是activity。属性是id(int primary key),type(varchar),description(varchar),city(varchar),location(varchar),date(varchar),price(varchar)。 成功.jsp

<%-- 
Document   : success
Created on : 19.Ara.2016, 12:41:49
Author     : BURAK NURÇİÇEK
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body background="http://www.teamarking.com/barcode/bar_background.jpg"> 

<marquee><h2 style="color:RED">WELCOME TO ONLINE RESERVATION SYSTEM           </marquee></h2>

 <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>

</head>
<form method = "get" action = "search.jsp">
<table align="right">
    <tr><td><b><font color='#D18603'>Type :</b></td><td><input type="text" name="type" /></td></tr>
    <tr><td><b><font color='#D18603'>City :</b></td><td><input type="text" name="city" /></td></tr>
    <tr><td><b><font color='#D18603'>Location :</b></td><td><input type="text" name="location" /></td></tr>
    <tr><td><b><font color='#D18603'>Date(YYYY-MM-DD) :</b></td><td><select name='search'><option value='1'>Today</option><option value='2'>Tomorrow</option><option value='3'>This Weekend</option><option value='4'>Next Weekend</option><option value='5'>This week</option><<option value='6'>Next 14 Days</option><option value='7'>Next 30 Days</option></select></b>;
    <tr><td colspan="2" align="center"><input type="submit" value="submit" /> <input type="reset" value="reset" /></td></tr></td></tr>
    <%
        String x = (String) application.getAttribute("id");
    //out.println(x);
    %>

</table>
<table>

</table>  
</form>
<table align='left'>
   <ul>
    <li><%
        out.println("<a href='music.jsp'>Music</a>");
        %>
    </li>
    <li><%
        out.println("<a href='sport.jsp'>Sport</a>");
        %></li>
    <li> <%
        out.println("<a href='stage.jsp'>Stage</a>");
        %></li>
    <li> <%
        out.println("<a href='family.jsp'>Family</a>");
        %></li>
    <li> <%
        out.println("<a href='education.jsp'>Education</a>");
        %></li>
   </ul>
   <div class="landscape">


    <script language="javascript" src="http://is.sitekodlari.com/takvim5.js"></script>
    <script language="javascript" src="http://is.sitekodlari.com/paylas17.js"></script>



    <%
        String username = (String) request.getSession().getAttribute("username");
        out.println("<br> Welcome " + username);

        out.println(
                "<br><a href='logout.jsp'>Log out</a>");

        out.println(
                "<br><a href='myreservations.jsp'>My Reservations</a>");
    %>

 </table>
 </div>
 </body>
 </html>

search.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>




<!DOCTYPE html>
<html>
<body background="http://www.teamarking.com/barcode/bar_background.jpg"> 
    <form method="post" action="reservations.jsp">

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Book Ticket</title>
    </head>

    <center>    
        <table border="1" width="30%" height="30%">
            <th><font color='#D18603'>id</font>
            <th><font color='#D18603'>Type</font></th>
            <th><font color='#D18603'>Description</font></th>
            <th><font color='#D18603'>City</font></th>
            <th><font color='#D18603'>Location</font></th>
            <th><font color='#D18603'>Date</font></th>
            <th><font color='#D18603'>Price</font></th>
            <th><font color='#D18603'>Buy</font>





                <%
                    Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

                    Statement st = con.createStatement();
                    ResultSet rs;
                    /* ERROR IN PARAMETERS */           rs = st.executeQuery("select * from activities where type='"+request.getParameter(type)+"' AND city='"+request.getParameter(city)+"' AND location='"+request.getParameter(location)+"' AND date='"+request.getParameter(date)+"'");
                    while (rs.next()) {

                        out.println("<tr>");
                        out.println("<form action='reservations.jsp'>");
                        out.println("<td>" + rs.getString("id") + "<input type='hidden' name='id' value='" + rs.getString("id") + "'></td>");
                        out.println("<td>" + rs.getString("type") +"<input type='hidden' name='type' value='" + rs.getString("type") + "'></td>");
                        out.println("<td>" + rs.getString("description") + "<input type='hidden' name='description' value='" + rs.getString("description") + "'></td>");
                        out.println("<td>" + rs.getString("city") + "<input type='hidden' name='city' value='" + rs.getString("city") + "'></td>");
                        out.println("<td>" + rs.getString("location") + "<input type='hidden' name='location' value='" + rs.getString("location") + "'></td>");
                        out.println("<td>" + rs.getString("date") +"<input type='hidden' name='date' value='" + rs.getString("date") + "'></td>");
                        out.println("<td>" + rs.getString("price") + "<input type='hidden' name='price' value='" + rs.getString("price") + "'></td>");

                        out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'></form></b>");

                        out.println("</tr>");

                    }
                    st.close();

                %>


                </center>
        </table>

        <br>  <br><a href='success.jsp'>Back</a>
        <br><br><a href='logout.jsp'>Log out</a>
  </form>
  </body>
  </html>

我给出了这个错误。因为我的sql代码错了,无法得到这个值

【问题讨论】:

  • 将参数名称用双引号括起来。例如request.getParameter("city") 而不是 request.getParameter(city)
  • 我做到了。但它在 search.jsp 上返回空表
  • 在 ID 参数的search.jsp 页面中使用rs.getInt 而不是rs.getString

标签: java mysql sql-server jsp


【解决方案1】:

错误似乎是 JSP 页面本身没有编译。你有没有想过你写的代码出了什么问题。

错误说,无法识别变量类型、城市、位置。建议您检查对象HttpServletRequest的方法getParameter作为参数是什么。

其次,最佳实践是将数据库查询移动到 Java 类中,而不是将其放在 JSP 页面本身中。

【讨论】:

    【解决方案2】:

    你想使用字符串:

    rs = st.executeQuery("select * from activities where type='"+request.getParameter("type")+"' AND city='"+request.getParameter("city")+"' AND location='"+request.getParameter("location")+"' AND date='"+request.getParameter("date")+"'");
    

    另外,养成使用PreparedStatement 的习惯(其中一个原因是删除 SQL 注入漏洞),您可以在其中定义如下:

    PreparedStatement ps = con.prepareStatement("select * from activities where type=? AND city=? AND location=? AND date=?");
    
    ps.setString(1, request.getParameter("type"));
    ps.setString(2, request.getParameter("city"));
    ps.setString(3, request.getParameter("location"));
    ps.setString(4, request.getParameter("date"));
    
    rs = ps.executeQuery();
    

    最后,不推荐在 JSP 中使用 Java 代码。您应该尝试将您的项目建模为 MVC 形式。

    【讨论】:

    • 当我搜索时它在搜索 jsp 中返回空表
    • 我可以使用您的社交媒体帐户以便快速交谈吗?我认为问题出在日期上。我不知道如何解决我
    • 我无法在聊天中联系到你
    猜你喜欢
    • 1970-01-01
    • 2016-03-22
    • 2017-08-20
    • 1970-01-01
    • 2016-08-01
    • 2016-08-09
    • 2012-11-23
    相关资源
    最近更新 更多