gpt4 book ai didi

java - 在动态网页上从数据库中搜索数据

转载 作者:行者123 更新时间:2023-11-28 23:21:29 25 4
gpt4 key购买 nike

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

搜索.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代码是错误的,我无法得到这个值enter image description here

最佳答案

你想使用字符串:

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 注入(inject)漏洞的原因之一)的习惯,您可以在其中定义如下:

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 形式。

关于java - 在动态网页上从数据库中搜索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41417480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com