产品信息的查询
yonghuchaxun.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<h1>查询时,输入相关信息,点击对应查询方式</h1>
<hr/>
<form action="/binguan/product" method="post">
<label for="categoryId">categoryId:</label>
<input type="text" name="categoryId" id="categoryId" value="" />
<br/>
<label for="price">price:</label>
<input type="text" name="price" id="price" value="" />
<br/>
<input type="submit" name="action" value="viewbycategory">
<input type="submit" name="action" value="viewbyprice">
<input type="submit" name="action" value="viewmybooked">
<hr/>
<a href='userchange.html'>修改用户信息</a>
</form>
</body>
</html>

ProductServlet:
package javaee.ch7;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import java.io.*;
public class ProductServlet extends HttpServlet
{
Connection con;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String action=(String)request.getParameter("action");
String productId=(String)request.getParameter("productId");
String categoryId=(String)request.getParameter("categoryId");
String name=(String)request.getParameter("name");
String descn=(String)request.getParameter("descn");
String producer=(String)request.getParameter("producer");
String price=(String)request.getParameter("price");
if(action.equals("add"))
{
out.println("information"+name+descn);
String aaa="156";
try {
ResultSet rst=getContact(productId);
rst.next();
aaa=rst.getString("productId");
out.println("ok already!"+aaa);
}catch(Exception e) {
try
{
out.println("add");
addproduct(productId,categoryId,name,descn,producer,price);
out.println("成功!"+productId);
}
catch(Exception ee)
{
out.println("insert:"+productId);
}
out.println("just fine!"+aaa);
}
}else
if(action.equals("view"))
{
ResultSet rst=getContact(productId);
out.println("<table border=1><tr><td>num</td><td>categoryId</td><td>name</td><td>descn</td><td>producer</td><td>price</td></tr>");
try
{
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+rst.getString("productId")+"</td>");
out.println("<td>"+rst.getInt("categoryId")+"</td>");
out.println("<td>"+rst.getString("name")+"</td>");
out.println("<td>"+rst.getString("descn")+"</td>");
out.println("<td>"+rst.getString("producer")+"</td>");
out.println("<td>"+rst.getString("price")+"</td>");
out.println("</tr>");
}
out.println("</table>");
rst.close();
}
catch(Exception e)
{
}
}
if(action.equals("viewbycategory")) {
ResultSet rst=viewbycategory(categoryId);
out.println("<table border=1><tr><td>num</td><td>categoryId</td><td>name</td><td>descn</td><td>producer</td><td>price</td></tr>");
try
{
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+rst.getString("productId")+"</td>");
out.println("<td>"+rst.getInt("categoryId")+"</td>");
out.println("<td>"+rst.getString("name")+"</td>");
out.println("<td>"+rst.getString("descn")+"</td>");
out.println("<td>"+rst.getString("producer")+"</td>");
out.println("<td>"+rst.getString("price")+"</td>");
out.println("<td>");
out.println("<form action='/binguan/dinggou' method='get'>");
out.println("<input type='text' name='productId' value=" +rst.getString("productId") + ">");
out.println( "<input type='submit' name='action' value='view'>" + "</td>");
out.println("</form>");
out.println("</td>");
out.println("</tr>");
}
out.println("</table>");
rst.close();
}
catch(Exception e)
{
}
}else
if(action.equals("viewbyprice")) {
ResultSet rst=viewbyprice(price);
out.println("<table border=1><tr><td>num</td><td>categoryId</td><td>name</td><td>descn</td><td>producer</td><td>price</td></tr>");
try
{
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+rst.getString("productId")+"</td>");
out.println("<td>"+rst.getInt("categoryId")+"</td>");
out.println("<td>"+rst.getString("name")+"</td>");
out.println("<td>"+rst.getString("descn")+"</td>");
out.println("<td>"+rst.getString("producer")+"</td>");
out.println("<td>"+rst.getString("price")+"</td>");
out.println("<td>");
out.println("<form action='/binguan/dinggou' method='get'>");
out.println("<input type='text' name='productId' value=" +rst.getString("productId") + ">");
out.println( "<input type='submit' name='action' value='view'>" + "</td>");
out.println("</form>");
out.println("</td>");
out.println("</tr>");
}
out.println("</table>");
rst.close();
}
catch(Exception e)
{
}
}else
if(action.equals("viewmybooked"))
{
request.getRequestDispatcher("wodedingdan.jsp").forward(request, response);
}
else
if(action.equals("change"))
{
out.println("kaishi");
try {
Statement stm=con.createStatement();
stm.executeUpdate("update products set descn="+descn+" where productId='"+productId+"'");
out.println("saved");
stm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.println("notsaved,error");
}
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
//创建一个Connection对象,用于执行数据库操作。
public ProductServlet()
{
this.con=DataBaseConnection.getConnection();
}
//在数据库中添加信息
public void addproduct(String productId,String categoryId,String name, String descn,String producer,String price)throws Exception
{
/*try
{
Statement stmt=con.createStatement();
stmt.execute("delete from user where name='"+userName+"'");
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}*/
//price=Intger.
int money=Integer.parseInt(price);
try
{
PreparedStatement pstmt=con.prepareStatement("insert into products values(?,?,?,?,?,?,?,?,?,?,?)");
pstmt.setString(1,productId);
pstmt.setString(2,categoryId);
pstmt.setString(3,name);
pstmt.setString(4,descn);
pstmt.setString(5,producer);
pstmt.setInt(6,money);
pstmt.setInt(7,1);
pstmt.setInt(8,1);
pstmt.setInt(9,1);
pstmt.setInt(10,1);
pstmt.setInt(11,1);
pstmt.execute();
//pstmt.setDate(4,new java.sql.Date(new java.util.Date().getTime()));
//con.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//查询数据库
public ResultSet getContact(String productId)
{
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select * from products where productId='"+productId+"'");
return result;
}
catch(Exception e)
{
}
return null;
}
//查询数据库
public ResultSet viewbycategory(String categoryId)
{
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select * from products where categoryId='"+categoryId+"'");
return result;
}
catch(Exception e)
{
}
return null;
}
public ResultSet viewbyprice(String price)
{
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select * from products where price<='"+price+"'");
return result;
}
catch(Exception e)
{
}
return null;
}
}
if(action.equals(“viewmybooked”))
{
request.getRequestDispatcher(“wodedingdan.jsp”).forward(request, response);
}
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*,java.io.*"%>
<html>
<body>
<body>
以下是从Mssql数据库读取的用户数据:
<hr>
<table border=1>
<tr>
<td>姓名</td>
<td>性别</td>
<td>key</td>
<td>电话</td>
<td>备注</td>
</tr>
<%
try {
//装载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//创建连接
Connection con = java.sql.DriverManager
.getConnection("jdbc:mysql://127.0.0.1:3306/binguan?serverTimezone=UTC",
"root", "123456");
String nname=(String)request.getSession().getAttribute("username"); // 获取
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("select * from book where uname="+nname);
while (rst.next()) {
out.println("<tr>");
out.println("<td>" + rst.getString("uname") + "</td>");
out.println("<td>" + rst.getString("day") + "</td>");
out.println("<td>" + rst.getString("product") + "</td>");
out.println("<td>" + rst.getString("date") + "</td>");
out.println("<td>" + "<input type='button' οnclick='login()' value='shenqingtuikuan'>" + "</td>");
out.println("</tr>");
}
//关闭连接、释放资源
rst.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
<script>
document.onkeydown = keyDown;
var event = event || window.event; // 标准化事件对象
function keyDown(event){
switch(event.keyCode){
case 13:
console.log("enter");
window.open("http://3245851ew7.qicp.vip/binguan/useBean.jsp");
break;
case 37 : // 按下左箭头键,向左移动5个像素
console.log(123123);
break;
}
}
//将输入框的值赋给div标签
function login(){
alert("kefunumber:1*************");
}
</script>
</body>
</html>
本文介绍了一种基于Web的应用程序,该程序允许用户通过不同的参数查询产品信息,包括类别ID和价格。此外,还提供了查看已预订产品的功能,并展示了从数据库读取的用户预订详情。
2617

被折叠的 条评论
为什么被折叠?



