📄 shopcart.jsp
字号:
<%@ page language="java" import="java.util.*,java.lang.*,java.sql.*"
pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>购物车</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">
<!--
body {
background-image: url(new/bg-0.jpg);
}
body,td,th {
font-size: 18px;
color: #9900CC;
}
a:link {
text-decoration: none;
color: #FF00FF;
}
a:visited {
text-decoration: none;
color: #FF00FF;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
-->
</style></head>
<body>
<p align="center"><img src="img/3.gif" width="612" height="32"> </p>
<p> </p>
<p>
<%
if (session.getAttribute("username") == null
|| session.getAttribute("username") == "") {
response.sendRedirect("relogin.html");
}
%>
<%
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
String goodsname;
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPasswd = "ptnest";
String url = "jdbc:mysql://127.0.0.1/bookstore?useUnicode=true&characterEncoding=ISO-8859-1";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
System.out.print("数据库类没有找到" + e.getMessage());
}
Connection conn = DriverManager.getConnection(url, userName,
userPasswd);
Statement stmt = conn.createStatement();
goodsname = new String(request.getParameter("CN").getBytes("iso8859_1"),"gb2312");
String sql1 = "select *from orderlist where goodsname='"+ goodsname
+ "'and user='" + session.getAttribute("username") + "'";
int search1 = 0;
try
{
ResultSet rs = stmt.executeQuery(sql1);
while (rs.next()) {
search1=search1+1;
}
}
catch(Exception e)
{
e.printStackTrace();
}
if (search1 == 0) {
String sqlCart = "insert into orderlist(user,goodsname,goodsnumber)Values ('"
+ session.getAttribute("username")
+ "','"
+ goodsname
+ "',1)";
try {
int num1 = stmt.executeUpdate(sqlCart);
out.println("<table width=800 align=center border=0>");
out.println("<tr>");
out.println("<td>");
out.println("<div align=center><font color='red' >成功加入购物车</font></div> ");
out.println("</td>");
out.println("</table>");
} catch (SQLException ex) {
out.println("<table width=800 border=0 align=center>");
out.println("<tr>");
out.println("<td>");
out.println("<div align='center' ><font color='red'>加入购物车失败</font></div>");
out.println(ex.toString());
out.println("</td>");
out.println("</tr>");
out.println("</table>");
}
} else {
String sqlCart = "update orderlist set goodsnumber=goodsnumber+1 where goodsname='"
+ goodsname
+ "'and user='"
+ session.getAttribute("username") + "'";
try {
int num1 = stmt.executeUpdate(sqlCart);
out.println("<table width=800 align=center border=0>");
out.println("<tr>");
out.println("<td>");
out
.println("<div align=center><font color='red' >成功加入购物车</font></div> ");
out.println("</td>");
out.println("</table>");
} catch (SQLException ex) {
out.println("<table width=800 border=0 align=center>");
out.println("<tr>");
out.println("<td>");
out
.println("<div align='center' ><font color='red'>加入购物车失败</font></div>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
}
}
%>
</p>
<p align=center> <a href=javascript:history.back()>返回购物</a> <a href=files/shopcartlist.jsp>购物车</a>
<a href="index.htm">返回主页</a>
</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -