java功能jsp实战
程序员文章站
2022-06-01 22:43:58
...
购物车:
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>在线购物</title>
</head>
<%
//装载MySQL5.5的JDBC驱动
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立数据库连接
//String url ="jdbc:mysql://localhost:3306/eshop?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
//Connection conn= DriverManager.getConnection(url);
/*sqlsever2010链接方法(注意:SQLsever老版本的链接方法中sqlserver.jdbc的位置)*/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=eshop","sa","123");
//建立一个Statement对象,用于执行SQL语句
Statement stat = con.createStatement();
//执行查询并得到查询结果
String sql = "select * from shop";
ResultSet rs = stat.executeQuery(sql);
%>
<body>
<h2>在线购物</h2>
<hr>
<table border="1" width="600">
<tr bgcolor="#dddddd">
<td align="center" width="80">商品缩图</td>
<td align="center">商品摘要</td>
<td align="center" width="100">在线购买</td>
</tr>
<%
String bm,name,price,info,img;
/*将查询结果集中的记录输出到页面上(全用String型,包括要发送的请求bm)*/
while (rs.next()){
//从当前记录中读取各字段的值
img = rs.getString("SP_PIC").trim();
bm = rs.getString("SP_NO");
name = rs.getString("SP_NAME").trim();
price = rs.getString("SP_PRICE").trim();
info = rs.getString("SP_INFO").trim();
out.println("<tr>");
out.println("<td><img src='"+ img +"' border=0 height=60 width=60></td>");
out.println("<td>");
out.println("商品编号:"+ bm +"<br>");
out.println("商品名称:"+ name +"<br>");
out.println("商品价格:"+ price +"元<br>");
out.println("商品简介:"+ info +"<br>");
out.println("</td>");
out.println("<td><a href='buy.jsp?op=add&bm="+bm+"'>放入购物车</a></td>");
out.println("</tr>");
}
%>
</table>
<br>
<a href="cart.jsp">查看购物车</a>
<a href="buy.jsp?op=clear">清空购物车</a>
</body>
</html>
cart.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>我的购物车</title>
</head>
<%
//装载MySQL5.5的JDBC驱动
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立数据库连接
//String url ="jdbc:mysql://localhost:3306/eshop?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
//Connection conn= DriverManager.getConnection(url);
//建立一个Statement对象,用于执行SQL语句
//Statement stat = conn.createStatement();
/*sqlsever2010链接方法(注意:SQLsever老版本的链接方法中sqlserver.jdbc的位置)*/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=eshop","sa","123");
//建立一个Statement对象,用于执行SQL语句
Statement stat = con.createStatement();
//执行查询并得到查询结果
String sql = "select * from cart";
ResultSet rs = stat.executeQuery(sql);
%>
<body>
<h2>我的购物车</h2>
<hr>
<table border="1" width="600">
<tr bgcolor="#dddddd">
<td align="center" width="80">商品名称</td>
<td align="center">商品单价</td>
<td align="center" width="100">购买数量</td>
<td align="center" width="100">金额</td>
<td align="center" width="100">编辑</td>
</tr>
<%
String id,name,price,num,count;
//将查询结果集中的记录输出到页面上
while (rs.next()){
//从当前记录中读取各字段的值
id = rs.getString("ID").trim();
name = rs.getString("SP_NAME").trim();
price = rs.getString("SP_PRICE").trim();
num = rs.getString("BUY_NUM").trim();
count = rs.getString("COUNT").trim();
out.println("<tr>");
out.println("<td>"+ name +"</td>");
out.println("<td>"+ price +"</td>");
out.println("<td><input type=text value="+ num +" onChange=\"updateNum('"+id+"',this.value,'"+price+"')\"></td>");
out.println("<td>"+ count +"</td>");
out.println("<td><a href='buy.jsp?op=del&id="+id+"'>退回商品架</a></td>");
out.println("</tr>");
}
%>
</table>
<br>
<a href="index.jsp">继续购物</a>
<a href="buy.jsp?op=clear">清空购物车</a>
</body>
<script language="javascript">
function updateNum(id,num,price){
var url = "buy.jsp?op=update&id="+id+"&num="+num+"&price="+price;
window.location = url;
}
</script>
</html>
buy.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*"%>
<%
//装载MySQL5.5的JDBC驱动
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立数据库连接
//String url ="jdbc:mysql://localhost:3306/eshop?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
//Connection conn= DriverManager.getConnection(url);
/*sqlsever2010链接方法(注意:SQLsever老版本的链接方法中sqlserver.jdbc的位置)*/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=eshop","sa","123");
//建立一个Statement对象,用于执行SQL语句
//Statement stat = con.createStatement();
PreparedStatement pstat = null; //实例化对象
ResultSet rs = null; //处理结果
String sql = null; //执行的SQL语句
String op = request.getParameter("op"); //处理方式
//将商品放入购物车
if (op.equals("add")){
//从商品库中取出所选购商品的数据
//String bm = request.getParameter("bm");
/*发送过来的数据改为与数据库中的数据类型相对应的形式*/
int bm = Integer.parseInt(request.getParameter("bm"));
int id1=0;
String name=null,price=null;
sql = "select * from shop where SP_NO=?";
pstat = conn.prepareStatement(sql);
pstat.setInt(1,bm);
rs = pstat.executeQuery();
if (rs.next()){
id1=rs.getInt("SP_NO");
name = rs.getString("SP_NAME").trim();
price = rs.getString("SP_PRICE").trim();
}
rs.close();
pstat.close();
//将所选购商品加入到购物车中
sql = "insert into cart(ID,SP_NAME,SP_PRICE,BUY_NUM,COUNT) values(?,?,?,?,?)";
pstat = conn.prepareStatement(sql);
pstat.setInt(1,id1);
pstat.setString(2,name);
pstat.setString(3,price);
pstat.setInt(4,1);
pstat.setString(5,price);
pstat.executeUpdate();
pstat.close();
//关闭数据库连接
conn.close();
//重定向到购物车页面
response.sendRedirect("cart.jsp");
}
//更改商品的数量
if (op.equals("update")){
int id = Integer.parseInt(request.getParameter("id"));
int num = Integer.parseInt(request.getParameter("num"));
double price=Double.parseDouble(request.getParameter("price"));
sql = "update cart set BUY_NUM = ?, COUNT = ? where ID=?";
pstat = conn.prepareStatement(sql);
pstat.setInt(1,num);
pstat.setString(2,new Double(price*num).toString());
pstat.setInt(3,id);
pstat.executeUpdate();
pstat.close();
//关闭数据库连接
conn.close();
//重定向到购物车页面
response.sendRedirect("cart.jsp");
}
//将商品退回到商品架(将商品从购物车中删除)
if (op.equals("del")){
int id = Integer.parseInt(request.getParameter("id"));
sql = "delete from cart where ID=?";
pstat = conn.prepareStatement(sql);
pstat.setInt(1,id);
pstat.executeUpdate();
pstat.close();
//关闭数据库连接
conn.close();
//重定向到购物车页面
response.sendRedirect("cart.jsp");
}
//清空购物车
if (op.equals("clear")){
sql = "delete from cart";
pstat = conn.prepareStatement(sql);
pstat.executeUpdate();
pstat.close();
//关闭数据库连接
conn.close();
//重定向到购物车页面
response.sendRedirect("cart.jsp");
}
%>
上一篇: mysql锁-MyISAM存储引擎
下一篇: 外接键盘坏了自己怎么维修?