├── web ├── images │ ├── s1.jpg │ ├── s2.jpg │ ├── s3.jpg │ ├── s4.jpg │ ├── left.png │ ├── right.png │ ├── 1482219425879.jpg │ ├── 1482219447332.jpg │ ├── 1482219468785.jpg │ ├── 1482219490238.jpg │ └── 1482219511691.jpg ├── todo.html ├── WEB-INF │ └── web.xml ├── my_order.jsp ├── css │ ├── page.css │ ├── cart.css │ ├── good.css │ ├── good_index.css │ ├── user_comm.css │ ├── header.css │ └── scroll.css ├── js │ ├── user_comm.js │ └── scroll.js ├── user_error.html ├── login.html ├── register.html ├── good.html ├── index.jsp ├── good.jsp ├── user_center.jsp ├── all_goods.jsp └── my_cart.jsp ├── README.md ├── src ├── db │ ├── FunDelete.java │ ├── PoolConn.java │ └── ConnectionPool.java └── servlet │ ├── Delete.java │ ├── Register.java │ ├── Ad_cart.java │ ├── Login.java │ └── Buy.java └── SQL.sql /web/images/s1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/s1.jpg -------------------------------------------------------------------------------- /web/images/s2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/s2.jpg -------------------------------------------------------------------------------- /web/images/s3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/s3.jpg -------------------------------------------------------------------------------- /web/images/s4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/s4.jpg -------------------------------------------------------------------------------- /web/images/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/left.png -------------------------------------------------------------------------------- /web/images/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/right.png -------------------------------------------------------------------------------- /web/images/1482219425879.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/1482219425879.jpg -------------------------------------------------------------------------------- /web/images/1482219447332.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/1482219447332.jpg -------------------------------------------------------------------------------- /web/images/1482219468785.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/1482219468785.jpg -------------------------------------------------------------------------------- /web/images/1482219490238.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/1482219490238.jpg -------------------------------------------------------------------------------- /web/images/1482219511691.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/changqing18/Shopping/HEAD/web/images/1482219511691.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Shopping 2 |

一个小型购物网站,基于Java和MySQL

3 | 在src/db/PoolConn.java中要写入数据库相关信息,数据库地址,用户名,密码 4 | 5 | -------------------------------------------------------------------------------- /web/todo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TODO 6 | 7 | 8 |




9 |

该功能尚未实现,敬请期待!

10 | 11 | -------------------------------------------------------------------------------- /web/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /web/my_order.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | Created by IntelliJ IDEA. 3 | User: 28713 4 | Date: 2016/12/27 5 | Time: 17:40 6 | To change this template use File | Settings | File Templates. 7 | --%> 8 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 | 10 | 11 | 我的订单 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /web/css/page.css: -------------------------------------------------------------------------------- 1 | #page{ 2 | width: 1210px; 3 | height: 30px; 4 | background-color: #2f2f2f; 5 | margin: 20px auto; 6 | } 7 | 8 | #page>a{ 9 | margin: 4px auto; 10 | padding: 0 10px; 11 | height:22px; 12 | line-height:22px; 13 | font-size: 22px; 14 | text-decoration: none; 15 | color: #ccc; 16 | display: block; 17 | float: left; 18 | } 19 | #page>a:hover{ 20 | color: #fff; 21 | } 22 | -------------------------------------------------------------------------------- /web/css/cart.css: -------------------------------------------------------------------------------- 1 | form { 2 | width: 1210px; 3 | margin: auto 62px; 4 | } 5 | table{ 6 | width:1210px; 7 | font-family: 等线; 8 | } 9 | tr { 10 | background-color: #fcfcfc; 11 | } 12 | td>a { 13 | text-decoration: none; 14 | color: #666; 15 | display: block; 16 | } 17 | td>a:hover { 18 | color: #C81623; 19 | } 20 | .gname{ 21 | margin: 8px 15px 17px 17px; 22 | width: 300px; 23 | } 24 | .stock{ 25 | color: red; 26 | } -------------------------------------------------------------------------------- /src/db/FunDelete.java: -------------------------------------------------------------------------------- 1 | package db; 2 | 3 | /** 4 | * Created by 28713 on 2016/12/28. 5 | */ 6 | public class FunDelete { 7 | public String[][] TranString(String[] array){ 8 | String [][] arr=new String[array.length][3]; 9 | for (int i = 0; i < array.length; i++) { 10 | arr[i][0]="orderid"+array[i]; 11 | arr[i][1]="gid"+array[i]; 12 | arr[i][2]="number"+array[i]; 13 | } 14 | return arr; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /web/css/good.css: -------------------------------------------------------------------------------- 1 | #good{ margin-top: 30px;} 2 | .radio{ 3 | list-style: none; 4 | } 5 | #image { 6 | width: 221px; 7 | height: 221px; 8 | margin-left: 61px; 9 | border: 1px solid #ddd; 10 | float: left; 11 | } 12 | 13 | #form { 14 | float:left; 15 | } 16 | 17 | #gname { 18 | font-family: "微软雅黑"; 19 | font-size: 17px; 20 | margin: 10px auto; 21 | } 22 | 23 | #price { 24 | font-family: "Arial"; 25 | font-size: 30px; 26 | color: #C81623; 27 | margin: 10px auto; 28 | } 29 | 30 | form > ul { 31 | 32 | } 33 | 34 | form > ul > li { 35 | font-size: 12px; 36 | color: #838383; 37 | } -------------------------------------------------------------------------------- /web/js/user_comm.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by 28713 on 2016/12/11. 3 | */ 4 | function PreUrl() { 5 | var u = document.referrer; 6 | var date = new Date(); 7 | date.setMinutes(date.getMinutes() + 15); 8 | 9 | if (u == "") 10 | u = "/index.jsp"; 11 | 12 | if (u.indexOf("_error.html") == -1 && 13 | u.indexOf("register.html") == -1 && 14 | u.indexOf("login.html") == -1) { 15 | document.cookie = "preurl=\"" + u + "\"; expires=" + date.toUTCString() + "path=/"; 16 | } 17 | } 18 | function getRequest() { 19 | var Email = ""; 20 | var name = ""; 21 | var error=-1; 22 | var requests = location.search; 23 | if (requests.indexOf("?") != -1) { 24 | var str = requests.substr(1).split("&"); 25 | try { 26 | Email = decodeURIComponent(str[0].split("=")[1]); 27 | name = decodeURIComponent(str[1].split("=")[1]); 28 | error = str[2].split("=")[1]; 29 | }catch(ignored){ } 30 | } 31 | return [Email,name,error]; 32 | } -------------------------------------------------------------------------------- /web/css/good_index.css: -------------------------------------------------------------------------------- 1 | #nav > ul > li { 2 | display: inline; 3 | } 4 | #good > ul { 5 | margin: 20px auto; 6 | padding: 25px 0 30px; 7 | border: 1px solid #ddd; 8 | width: 1210px; 9 | height: 285px; 10 | } 11 | #good > ul > li { 12 | width: 241px; 13 | height: 100%; 14 | margin: auto; 15 | float: left; 16 | } 17 | #good>ul>li:nth-child(even){ 18 | border-left:1px solid #ddd; 19 | border-right: 1px solid #ddd; 20 | } 21 | .dimage { 22 | width: 100%; 23 | height: 206px; 24 | line-height: 206px; 25 | text-align: center; 26 | } 27 | .gimage { 28 | vertical-align: middle; 29 | width: 180px; 30 | height: 180px; 31 | } 32 | .gname { 33 | margin: 8px 15px 17px 17px; 34 | font-family: "Microsoft Yahei"; 35 | width: 200px; 36 | text-decoration: none; 37 | color: #666; 38 | font-size: 12px; 39 | display: block; 40 | } 41 | .gname:hover { 42 | color: #C81623; 43 | } 44 | .price { 45 | font-size: 18px; 46 | font-family: "Microsoft Yahei"; 47 | line-height: 18px; 48 | margin-left: 17px; 49 | color: #c61822; 50 | } -------------------------------------------------------------------------------- /web/css/user_comm.css: -------------------------------------------------------------------------------- 1 | p{ 2 | font-family: "微软雅黑", "黑体", "Arial"; 3 | font-size:200%; 4 | text-align: center; 5 | } 6 | ol{ 7 | list-style:none; 8 | margin:0; 9 | padding:0; 10 | } 11 | ol li{ 12 | clear:both; 13 | margin:0; 14 | padding:0; 15 | } 16 | label{ 17 | float:left; 18 | } 19 | input{ 20 | border:1px solid #333; 21 | height:25px; 22 | margin:2px 0; 23 | font-size:100%; 24 | } 25 | input[required]:invalid,input[pattern]:invalid{ 26 | border-color:#a5340b; 27 | } 28 | input[required]:valid,input[pattern]:valid{ 29 | border-color:#0b9900; 30 | } 31 | input:focus{background-color: #ffe;} 32 | .simpleColorContainer,.simpleColorDisplay{ 33 | float:left; 34 | } 35 | form{ 36 | font-family: "微软雅黑", "黑体", "Arial"; 37 | font-size:120%; 38 | margin:30px auto; 39 | } 40 | a{ font-size:14px;} 41 | #submit{ 42 | display:block; 43 | margin:auto; 44 | background-color: #0be500; 45 | height:34px; 46 | font-size: 25px; 47 | font-family: "微软雅黑", "黑体", "Arial"; 48 | } 49 | input,fieldset,legend{ 50 | border-radius:6px; 51 | } 52 | fieldset{ 53 | background-color: #ddd; 54 | } -------------------------------------------------------------------------------- /web/css/header.css: -------------------------------------------------------------------------------- 1 | ul,li{ list-style: none; } 2 | header{ 3 | color: #2f2f2f; 4 | padding-left: 61px; 5 | font-family: 微软雅黑; 6 | font-size: 14px; 7 | } 8 | header>a{ 9 | color: #2f2f2f; 10 | text-decoration: none; 11 | font-size: 14px; 12 | } 13 | header>a:hover{ 14 | color: #C81623; 15 | } 16 | #nav{ 17 | width: 1210px; 18 | height: 54px; 19 | background: #393838; 20 | margin: auto 61px; 21 | } 22 | #nav>ul>li{ 23 | vertical-align: middle; 24 | } 25 | #nav>ul>li>a{ 26 | display: block; 27 | padding: 0 70px; 28 | height:54px; 29 | line-height:54px; 30 | font-size: 20px; 31 | text-decoration: none; 32 | text-align: center; 33 | color: #ccc; 34 | float: left; 35 | } 36 | #nav>ul>li>a:hover{ 37 | color: #fff; 38 | } 39 | #page_footer{ 40 | width: 1210px; 41 | margin: auto 61px; 42 | font-family: Arial; 43 | background:#393838; 44 | color:#ccc; 45 | clear: both; 46 | display:block; 47 | text-align: center; 48 | } 49 | #page_footer>nav>ul>li>a{ 50 | font-family: Arial; 51 | color:#ccc; 52 | } 53 | #page_footer>nav>ul{ 54 | list-style:none; 55 | margin: 0; 56 | padding: 0; 57 | } 58 | #page_footer>nav>ul>li{ 59 | line-height: 20px; 60 | margin: 0 20px 0 0; 61 | padding:0; 62 | display:inline; 63 | } -------------------------------------------------------------------------------- /src/servlet/Delete.java: -------------------------------------------------------------------------------- 1 | package servlet; 2 | 3 | import db.PoolConn; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | import java.sql.Connection; 12 | import java.sql.PreparedStatement; 13 | import java.sql.SQLException; 14 | 15 | /** 16 | * Created by 28713 on 2016/12/27. 17 | */ 18 | @WebServlet(value = "/servlet/Delete", name = "Delete") 19 | public class Delete extends HttpServlet { 20 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 | String gid = request.getParameter("gid"); 22 | 23 | String sql = "DELETE FROM cart WHERE gid=?"; 24 | PoolConn poolConn = PoolConn.getPoolConn(); 25 | try (Connection con = poolConn.getConnection(); 26 | PreparedStatement statement = con.prepareStatement(sql)) { 27 | if (gid != null) { 28 | statement.setString(1, gid); 29 | statement.executeUpdate(); 30 | response.sendRedirect("/user_error.html?Email=&name=&&error=50"); 31 | } else { 32 | response.sendRedirect("/user_error.html?Email=&name=&&error=52"); 33 | } 34 | } catch (SQLException e) { 35 | e.printStackTrace(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/db/PoolConn.java: -------------------------------------------------------------------------------- 1 | package db; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.SQLException; 6 | 7 | /** 8 | * Created by 28713 on 2016/12/17. 9 | */ 10 | public class PoolConn{ 11 | 12 | private static final ConnectionPool connection = new ConnectionPool( 13 | "com.mysql.jdbc.Driver", "", 14 | "", ""); 15 | 16 | private static final PoolConn poolConn = new PoolConn(); 17 | 18 | public ConnectionPool getPool() { 19 | return connection; 20 | } 21 | 22 | public Connection getConnection() throws SQLException { 23 | try { 24 | connection.createPool(); 25 | } catch (Exception e) { 26 | e.printStackTrace(); 27 | } 28 | return connection.getConnection(); 29 | } 30 | 31 | public void returnConnection(Connection conn) { 32 | connection.returnConnection(conn); 33 | } 34 | 35 | public static PoolConn getPoolConn() { 36 | return poolConn; 37 | } 38 | 39 | public int preparedStatement(String sql,String[] array) throws SQLException{ 40 | Connection conn=poolConn.getConnection(); 41 | PreparedStatement statement=conn.prepareStatement(sql); 42 | for (int i = 0; i < array.length; i++) { 43 | statement.setString(i+1,array[i]); 44 | } 45 | int i=statement.executeUpdate(); 46 | statement.close(); 47 | poolConn.returnConnection(conn); 48 | conn.close(); 49 | return i; 50 | } 51 | } -------------------------------------------------------------------------------- /web/css/scroll.css: -------------------------------------------------------------------------------- 1 | .lyc-container, .lyc-out { 2 | position: relative; 3 | overflow: hidden; 4 | } 5 | 6 | .lyc-container { 7 | width: 1210px; 8 | margin: 20px auto ; 9 | } 10 | 11 | .lyc-out { 12 | margin: 0 auto; 13 | } 14 | 15 | .lyc-out, .lyc-inner img { 16 | width: 1210px; 17 | height: 450px; 18 | } 19 | 20 | .lyc-inner { 21 | margin: 0; 22 | padding: 0; 23 | list-style: none; 24 | width: 1210px; 25 | height: 450px; 26 | overflow: hidden; 27 | } 28 | 29 | .lyc-inner li, .lyc-imgs_indexs li { 30 | float: left; 31 | list-style: none; 32 | } 33 | 34 | .lyc-inner img { 35 | float: left; 36 | } 37 | 38 | .lyc-imgs_indexs { 39 | position: absolute; 40 | bottom: 20px; 41 | right: 20px; 42 | } 43 | 44 | .lyc-imgs_indexs li { 45 | width: 30px; 46 | height: 30px; 47 | margin-left: 10px; 48 | color: white; 49 | font-size: 20px; 50 | text-align: center; 51 | line-height: 30px; 52 | background-color: #c8c8c8; 53 | border-radius: 100%; 54 | cursor: pointer; 55 | } 56 | 57 | .lyc-imgs_indexs li:hover { 58 | background-color: #343434; 59 | } 60 | 61 | .lyc-left, .lyc-right { 62 | position: absolute; 63 | top: 180px; 64 | width: 25px; 65 | height: 24px; 66 | vertical-align: middle; 67 | cursor: pointer; 68 | z-index: 10; 69 | } 70 | 71 | .lyc-left { 72 | left:20px; 73 | background: url(../images/left.png) no-repeat; 74 | } 75 | 76 | .lyc-right { 77 | right: 20px; 78 | background: url(../images/right.png) no-repeat; 79 | } -------------------------------------------------------------------------------- /web/user_error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | LoginError 6 | 7 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /src/servlet/Register.java: -------------------------------------------------------------------------------- 1 | package servlet; 2 | 3 | import db.PoolConn; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | import java.sql.Connection; 12 | import java.sql.PreparedStatement; 13 | import java.sql.SQLException; 14 | 15 | /** 16 | * Created by 28713 on 2016/12/10. 17 | */ 18 | @WebServlet(value = "/servlet/Register", name = "Register") 19 | public class Register extends HttpServlet { 20 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 | 22 | String name = request.getParameter("name"); 23 | String email = request.getParameter("email"); 24 | String password = request.getParameter("password"); 25 | 26 | String sql = "insert into user values(?,?,?)"; 27 | String[] array = {name, email, password}; 28 | 29 | try { 30 | int i=PoolConn.getPoolConn().preparedStatement(sql,array); 31 | if (i== 1) { 32 | response.sendRedirect("/user_error.html?Email=" + email + "&name=" + name + "&error=10"); 33 | } else { 34 | response.sendRedirect("/user_error.html?Email=" + email + "&name=" + name + "&error=12"); 35 | } 36 | } catch (SQLException e) { 37 | if(e.getMessage().contains("Duplicate entry")) 38 | response.sendRedirect("/user_error.html?Email=" + email + "&name=" + name + "&error=11"); 39 | else { 40 | e.printStackTrace(); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /web/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 登录 6 | 12 | 13 | 14 | 15 | 16 |
17 |
18 |

登录

19 |
20 | 21 |
22 |
    23 |
  1. 24 | 25 | 26 |
  2. 27 | 28 |
  3. 29 | 30 | 31 |
  4. 32 |
  5. 33 | 忘记密码? 34 | 还未注册? 35 |
  6. 36 |
  7. 37 | 38 |
39 |
40 |
41 | 60 | 61 | -------------------------------------------------------------------------------- /src/servlet/Ad_cart.java: -------------------------------------------------------------------------------- 1 | package servlet; 2 | 3 | import db.PoolConn; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.*; 8 | import java.io.IOException; 9 | import java.sql.CallableStatement; 10 | import java.sql.Connection; 11 | import java.sql.SQLException; 12 | 13 | /** 14 | * Created by 28713 on 2016/12/26. 15 | */ 16 | @WebServlet(value = "/servlet/Ad_cart", name = "Ad_cart") 17 | public class Ad_cart extends HttpServlet { 18 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 19 | HttpSession session = request.getSession(); 20 | String Email = (String) session.getAttribute("Email"); 21 | if (Email == null) { 22 | response.sendRedirect("/user_error.html?Email=&name=&error=21"); 23 | } else { 24 | long gid = Long.parseLong(request.getParameter("gid")); 25 | int number = Integer.parseInt(request.getParameter("number")); 26 | PoolConn poolConn = PoolConn.getPoolConn(); 27 | try (Connection con = poolConn.getConnection(); 28 | CallableStatement cs = con.prepareCall("CALL cart_merge(?,?,?,?)")) { 29 | cs.setLong(1, System.currentTimeMillis()); 30 | cs.setString(2, Email); 31 | cs.setLong(3, gid); 32 | cs.setInt(4, number); 33 | int i = cs.executeUpdate(); 34 | poolConn.returnConnection(con); 35 | if (i == 1) 36 | response.sendRedirect("/user_error.html?Email=&name=&error=30"); 37 | else { 38 | response.sendRedirect("/user_error.html?Email=&name=&error=32"); 39 | } 40 | } catch (SQLException e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /SQL.sql: -------------------------------------------------------------------------------- 1 | create table border 2 | ( 3 | orderid bigint not null, 4 | email varchar(30) not null, 5 | gid bigint not null, 6 | number int not null, 7 | buytime datetime default CURRENT_TIMESTAMP not null, 8 | constraint `PRIMARY` 9 | primary key (orderid) 10 | ) 11 | ; 12 | 13 | create index gid 14 | on border (gid) 15 | ; 16 | 17 | create table cart 18 | ( 19 | orderid bigint not null, 20 | email varchar(30) not null, 21 | gid bigint not null, 22 | number int not null, 23 | constraint `PRIMARY` 24 | primary key (orderid) 25 | ) 26 | ; 27 | 28 | create index gid 29 | on cart (gid) 30 | ; 31 | 32 | create table good 33 | ( 34 | gid bigint not null, 35 | gname varchar(100) not null, 36 | brand varchar(10) null, 37 | price float(6,2) not null, 38 | number int not null, 39 | constraint `PRIMARY` 40 | primary key (gid) 41 | ) 42 | ; 43 | 44 | alter table border 45 | add constraint border_ibfk_1 46 | foreign key (gid) references web.good (gid) 47 | ; 48 | 49 | alter table cart 50 | add constraint cart_ibfk_1 51 | foreign key (gid) references web.good (gid) 52 | ; 53 | 54 | create table user 55 | ( 56 | username varchar(20) null, 57 | email varchar(30) not null, 58 | password varchar(20) not null, 59 | constraint `PRIMARY` 60 | primary key (email) 61 | ) 62 | ; 63 | 64 | create procedure buy (IN pid bigint, IN pe varchar(30), IN pgid bigint, IN num int) 65 | CREATE PROCEDURE `buy`(`pid` BIGINT(20), `pe` VARCHAR(30), `pgid` BIGINT(20), `num` INT(11)) 66 | BEGIN 67 | INSERT INTO border (orderid, email, gid, number) 68 | VALUES (pid, pe, pgid, num); 69 | DELETE FROM cart 70 | WHERE orderid = pid; 71 | UPDATE good 72 | SET number = number - num 73 | WHERE gid = pgid; 74 | END; 75 | 76 | create procedure cart_merge (IN pid bigint, IN pemail varchar(30), IN pgid bigint, IN num int) 77 | CREATE PROCEDURE `cart_merge`(`pid` BIGINT(20), `pemail` VARCHAR(30), `pgid` BIGINT(20), `num` INT(11)) 78 | BEGIN 79 | DECLARE temp INT; 80 | SELECT number 81 | INTO temp 82 | FROM cart 83 | WHERE gid = pgid && email = pemail; 84 | IF temp IS NULL 85 | THEN INSERT INTO cart VALUES (pid, pemail, pgid, num); 86 | ELSE UPDATE cart 87 | SET orderid = pid, number = num + temp 88 | WHERE gid = pgid && email = pemail; 89 | END IF; 90 | END; 91 | 92 | -------------------------------------------------------------------------------- /web/register.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 注册账号 6 | 11 | 12 | 13 | 14 | 15 |
16 |
17 |

注册账号

18 |
19 | 20 |
21 |
    22 |
  1. 23 | 24 | 25 |
  2. 26 | 27 |
  3. 28 | 29 | 30 |
  4. 31 | 32 |
  5. 33 | 34 | 38 |
  6. 39 |
  7. 40 | 41 | 44 |
  8. 45 |
  9. 已有账号?前往登录
  10. 46 |
  11. 47 | 48 |
49 |
50 |
51 | 66 | 67 | -------------------------------------------------------------------------------- /web/good.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 商品详情 6 | 7 | 8 | 9 | 10 |
11 | 请登录  请注册 12 |
13 | 22 |
23 |
24 | 25 |
26 |
27 |

中兴(ZTE) A2 Plus 4GB+32GB高配 流光金 全网通4G双卡双待

28 |

¥949.00

29 |
30 |
    31 |
  • 选择版本 32 | 33 | 34 |
  • 35 |
  • 选择颜色 36 | 37 | 38 | 39 | 40 |
  • 41 |
  • 套餐类型 42 | 43 | 44 |
  • 45 |
  • 储存容量 46 | 47 | 48 |
  • 49 |
  • 数量 50 | 51 |   库存200件 52 |
  • 53 |
  • 54 | 55 |
  • 56 |
57 |
58 |
59 |
60 | 61 | -------------------------------------------------------------------------------- /src/servlet/Login.java: -------------------------------------------------------------------------------- 1 | package servlet; 2 | 3 | import db.PoolConn; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.*; 8 | import java.io.IOException; 9 | import java.sql.Connection; 10 | import java.sql.PreparedStatement; 11 | import java.sql.ResultSet; 12 | import java.sql.SQLException; 13 | 14 | /** 15 | * Created by 28713 on 2016/12/10. 16 | */ 17 | @WebServlet(value = "/servlet/Login", name = "Login") 18 | public class Login extends HttpServlet { 19 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 | 21 | String email = request.getParameter("email"); 22 | String password = request.getParameter("password"); 23 | String sql = "SELECT password FROM user WHERE email=?"; 24 | PoolConn poolConn=PoolConn.getPoolConn(); 25 | try (Connection conn=poolConn.getConnection(); 26 | PreparedStatement statement = conn.prepareStatement(sql)) { 27 | 28 | statement.setString(1,email); 29 | ResultSet resultSet=statement.executeQuery(); 30 | poolConn.returnConnection(conn); 31 | if (!resultSet.next()) { 32 | //输出不存在该用户,并调回登录界面 连同之前进入登录界面的url,以及email 33 | resultSet.close(); 34 | response.sendRedirect("/user_error.html?Email=" + email + "&name=name&error=2"); 35 | } else if (!password.equals(resultSet.getString(1))) { 36 | //输出密码错误,返回登录界面,连同之前进入登录界面的url,以及email 37 | resultSet.close(); 38 | response.sendRedirect("/user_error.html?Email=" + email + "&name=name&error=1"); 39 | } else { 40 | resultSet.close(); 41 | Cookie[] cookies = request.getCookies(); 42 | String preUrl="/index.jsp"; 43 | for (Cookie cookie : cookies) { 44 | if (cookie.getName().equals("preurl")) { 45 | preUrl=cookie.getValue(); 46 | cookie.setMaxAge(0); 47 | cookie.setPath("/"); 48 | response.addCookie(cookie); 49 | break; 50 | } 51 | } 52 | Cookie cookie = new Cookie("user", email); 53 | cookie.setMaxAge(60*60*24*7); 54 | cookie.setPath("/"); 55 | response.addCookie(cookie); 56 | 57 | HttpSession session1 = request.getSession(); 58 | session1.setAttribute("Email",email); 59 | response.sendRedirect(preUrl); 60 | } 61 | } catch (SQLException e) { 62 | e.printStackTrace(); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /web/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page import="java.sql.PreparedStatement" %> 2 | <%@ page import="java.sql.SQLException" %> 3 | <%@ page import="java.sql.ResultSet" %> 4 | <%@ page import="java.sql.Connection" %> 5 | <%@ page import="db.PoolConn" %><%-- 6 | Created by IntelliJ IDEA. 7 | User: 28713 8 | Date: 2016/12/9 9 | Time: 22:30 10 | To change this template use File | Settings | File Templates. 11 | --%> 12 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 13 | 14 | 15 | 16 | 17 | 快乐购物 18 | 19 | 20 | 21 | 22 | 23 |
24 | <% 25 | session = request.getSession(); 26 | String Email = (String) session.getAttribute("Email"); 27 | if (Email == null) { 28 | %> 29 | 请登录  请注册 30 | <% 31 | } else { 32 | %> 33 | 欢迎您<%=Email%> 34 | 35 | <% 36 | } 37 | %> 38 |
39 | 48 |
49 |
50 | 57 |
58 | 64 |

65 |

66 |
67 | 68 |
69 | <% 70 | String sql = "SELECT gid,gname,price FROM good LIMIT 0,5"; 71 | PoolConn poolConn = PoolConn.getPoolConn(); 72 | try (Connection conn = poolConn.getConnection(); 73 | PreparedStatement statement = conn.prepareStatement(sql); 74 | ResultSet resultSet = statement.executeQuery() 75 | ) { 76 | %> 77 | 94 |
95 | 106 | <% 107 | poolConn.returnConnection(conn); 108 | } catch (SQLException e) { 109 | e.printStackTrace(); 110 | } 111 | %> 112 | 113 | 114 | -------------------------------------------------------------------------------- /web/good.jsp: -------------------------------------------------------------------------------- 1 | <%@ page import="db.PoolConn" %> 2 | <%@ page import="java.sql.Connection" %> 3 | <%@ page import="java.sql.PreparedStatement" %> 4 | <%@ page import="java.sql.ResultSet" %> 5 | <%@ page import="java.sql.SQLException" %><%-- 6 | Created by IntelliJ IDEA. 7 | User: 28713 8 | Date: 2016/12/25 9 | Time: 16:19 10 | To change this template use File | Settings | File Templates. 11 | --%> 12 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 13 | 14 | 15 | 16 | 商品详情 17 | 18 | 19 |
20 | <% 21 | session = request.getSession(); 22 | String Email = (String) session.getAttribute("Email"); 23 | if (Email == null) { 24 | %> 25 | 请登录  请注册 26 | <% 27 | } else { 28 | %> 29 | 欢迎您<%=Email%> 30 | 31 | <% 32 | } 33 | %> 34 |
35 | 44 | <% 45 | long gid = Long.parseLong(request.getParameter("gid")); 46 | String sql = "SELECT gname,price,number FROM good WHERE gid=?"; 47 | PoolConn poolConn = PoolConn.getPoolConn(); 48 | try (Connection con = poolConn.getConnection(); 49 | PreparedStatement statement = con.prepareStatement(sql)) { 50 | statement.setLong(1, gid); 51 | ResultSet resultSet = statement.executeQuery(); 52 | resultSet.next(); 53 | %> 54 |
55 |
56 | 57 |
58 |
59 |

<%=resultSet.getString(1)%>

60 |

¥<%=resultSet.getFloat(2)%>

61 |
62 |
    63 |
  • 选择版本: 64 | 65 | 66 | 67 |
  • 68 |
  • 选择颜色: 69 | 70 | 71 | 72 | 73 |
  • 74 |
  • 套餐类型: 75 | 76 | 77 |
  • 78 |
  • 储存容量: 79 | 80 | 81 |
  • 82 |
  • 数量 83 | 84 |    86 |
    87 | 库存<%=resultSet.getInt(3)%>件 88 |
    89 |
  • 90 |
  • 91 | 92 |
  • 93 |
94 |
95 |
96 |
97 | <% 98 | resultSet.close(); 99 | poolConn.returnConnection(con); 100 | } catch (SQLException e) { 101 | e.printStackTrace(); 102 | } 103 | %> 104 | 105 | 106 | -------------------------------------------------------------------------------- /web/js/scroll.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by 28713 on 2016/12/20. 3 | */ 4 | function Scroll(obj){ 5 | this.num = null;//滚动的页数 6 | this.width = 1210;//每一页的宽度 7 | this.height = 450;//每一页的高度 8 | this.n = 1;//当前页 9 | this.timer1 = null;//滚动一页的计时器 10 | this.timer2 = null;//自动滚动的计时器 11 | this.container = null;//滚动结构 12 | this.out = null;//滚动容器 13 | this.inner = null;//包裹容器 14 | this.left = null;//向左滚动按钮 15 | this.right = null;//向右滚动按钮 16 | this.imgs_indexs = null;//图片序号 17 | } 18 | Scroll.prototype.init = function(obj){ 19 | //初始化容器 20 | this.container = document.querySelector(obj); 21 | this.out = this.container.querySelector(".lyc-out"); 22 | this.inner = this.container.querySelector(".lyc-inner"); 23 | this.num = this.inner.querySelectorAll("li").length-1; 24 | this.inner.style.height = this.height+"px"; 25 | this.inner.style.width=this.width*(this.num+1)+"px"; 26 | this.imgs_indexs = this.container.querySelector(".lyc-imgs_indexs"); 27 | this.left = this.container.querySelector(".lyc-left"); 28 | this.right = this.container.querySelector(".lyc-right"); 29 | this.out.scrollLeft = this.width; 30 | //自动滚动开始 31 | this.automove(); 32 | }; 33 | Scroll.prototype.move = function(n){ 34 | var self = this; 35 | var step = 0; 36 | var maxstep = 20; 37 | var pos = self.out.scrollLeft; 38 | var end = this.width*n; 39 | var every = (end-pos)/maxstep; 40 | 41 | clearInterval(self.timer1); 42 | self.timer1 = setInterval(function(){ 43 | step++; 44 | if(step>=maxstep){ 45 | clearInterval(self.timer1); 46 | self.out.scrollLeft=end; 47 | step=0; 48 | } 49 | pos+=every; 50 | self.out.scrollLeft=pos; 51 | },10); 52 | }; 53 | Scroll.prototype.automove = function(){ 54 | var self = this; 55 | clearInterval(self.timer2); 56 | self.timer2=setInterval(function(){ 57 | self.n++; 58 | if(self.n>self.num){ 59 | self.n=1; 60 | self.out.scrollLeft=0; 61 | } 62 | self.move(self.n); 63 | self.current(); 64 | },2000); 65 | self.arrow(); 66 | self.tap(); 67 | }; 68 | Scroll.prototype.arrow = function(){ 69 | var self = this; 70 | if(!self.left){ 71 | return; 72 | } 73 | self.left.onclick=function(){ 74 | clearInterval(self.timer1); 75 | clearInterval(self.timer2); 76 | self.n--; 77 | if(self.n<0){ 78 | self.n=self.num-1; 79 | self.out.scrollLeft = self.width*self.num; 80 | } 81 | self.move(self.n); 82 | self.current(); 83 | self.automove(); 84 | }; 85 | self.right.onclick=function(){ 86 | clearInterval(self.timer1); 87 | clearInterval(self.timer2); 88 | self.n++; 89 | if(self.n>self.num){ 90 | self.n=1; 91 | self.out.scrollLeft = 0; 92 | } 93 | self.move(self.n); 94 | self.current(); 95 | self.automove(); 96 | } 97 | }; 98 | Scroll.prototype.tap = function(){ 99 | var self = this; 100 | if(!self.imgs_indexs){ 101 | return; 102 | } 103 | var imgs_index = self.imgs_indexs.getElementsByTagName("li"); 104 | for(var i=0;i= 0; i--) { 34 | orderID[i] = Long.parseLong(request.getParameter(Trans[i][0])); 35 | gid[i] = Long.parseLong(request.getParameter(Trans[i][1])); 36 | number[i] = Integer.parseInt(request.getParameter(Trans[i][2])); 37 | } 38 | int subtype = Integer.parseInt(request.getParameter("subtype")); 39 | System.out.println(subtype); 40 | PoolConn poolConn = PoolConn.getPoolConn(); 41 | 42 | if (subtype == 1) { 43 | String sql = "UPDATE cart SET number=?WHERE orderid=?"; 44 | try (Connection con = poolConn.getConnection(); 45 | PreparedStatement statement = con.prepareStatement(sql)) { 46 | for (int j = 0; j < Trans.length; j++) { 47 | statement.setInt(1, number[j]); 48 | statement.setLong(2, orderID[i]); 49 | statement.addBatch(); 50 | } 51 | statement.executeBatch(); 52 | response.sendRedirect("/user_error.html?Email=&name=&error=50"); 53 | } catch (SQLException e) { 54 | e.printStackTrace(); 55 | } 56 | } else { 57 | String sql = "SELECT number FROM good WHERE gid IN("; 58 | for ( 59 | int j = 0; 60 | j < gid.length - 1; j++) 61 | sql = sql + gid[j] + ","; 62 | sql = sql + gid[gid.length - 1] + ");"; 63 | String sql1 = "UPDATE cart SET number=? WHERE orderid=?"; 64 | try (Connection con = poolConn.getConnection(); 65 | Statement statement = con.createStatement(); 66 | PreparedStatement statement1 = con.prepareStatement(sql1); 67 | CallableStatement cs = con.prepareCall("CALL buy(?,?,?,?)")) { 68 | //先查询库存是否满足,好像效率有点低! 69 | ResultSet rs = statement.executeQuery(sql); 70 | int x = -1; 71 | while (rs.next()) { 72 | x++; 73 | if (rs.getInt(1) < gid[x]) { 74 | rs.close(); 75 | for (int j = 0; j < Trans.length; j++) {//库存不足时,更新数据,并提示不足,调回购物车 76 | statement1.setInt(1, number[j]); 77 | statement1.setLong(2, orderID[i]); 78 | statement1.addBatch(); 79 | } 80 | statement1.executeBatch(); 81 | response.sendRedirect("/user_error.html?Email=&name=&error=41"); 82 | } 83 | } 84 | rs.close(); 85 | //库存满足 执行存储过程(将数据插进border,并从cart中删除相应数据,从good中减去购买数量) 86 | for (int j = 0; j < Trans.length; j++) { 87 | cs.setLong(1, orderID[j]); 88 | cs.setString(2, Email); 89 | cs.setLong(3, gid[j]); 90 | cs.setInt(4, number[j]); 91 | cs.addBatch(); 92 | } 93 | cs.executeBatch(); 94 | response.sendRedirect("/user_error.html?Email=&name=&error=40"); 95 | 96 | } catch (SQLException e) { 97 | response.sendRedirect("/user_error.html?Email=&name=&error=42"); 98 | e.printStackTrace(); 99 | } 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /web/user_center.jsp: -------------------------------------------------------------------------------- 1 | <%@ page import="db.PoolConn" %> 2 | <%@ page import="java.sql.Connection" %> 3 | <%@ page import="java.sql.PreparedStatement" %> 4 | <%@ page import="java.sql.SQLException" %> 5 | <%@ page import="java.sql.ResultSet" %><%-- 6 | Created by IntelliJ IDEA. 7 | User: 28713 8 | Date: 2016/12/17 9 | Time: 22:42 10 | To change this template use File | Settings | File Templates. 11 | --%> 12 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 13 | 14 | 15 | 用户中心 16 | 17 | 41 | 52 | 53 | 54 | <% 55 | PoolConn poolConn = PoolConn.getPoolConn(); 56 | session = request.getSession(); 57 | String Email = (String) session.getAttribute("Email"); 58 | if (Email == null) { 59 | response.sendRedirect("user_error.html?Email=&name=&error=21"); 60 | } 61 | 62 | String username = request.getParameter("username"); 63 | 64 | if (username != null) { 65 | String sql = "update user set username=? where email=?"; 66 | String[] array = {username, Email}; 67 | int i = -1; 68 | try { 69 | i = poolConn.preparedStatement(sql, array); 70 | } catch (SQLException e) { 71 | e.printStackTrace(); 72 | } 73 | if (i != 1) { 74 | response.sendRedirect("user_error.html?Email=&name=&error=-1"); 75 | } 76 | } else { 77 | String sql = "SELECT username FROM user WHERE email=?"; 78 | try (Connection conn = poolConn.getConnection(); 79 | PreparedStatement statement = conn.prepareStatement(sql)) { 80 | statement.setString(1, Email); 81 | ResultSet resultSet = statement.executeQuery(); 82 | resultSet.next(); 83 | username = resultSet.getString(1); 84 | resultSet.close(); 85 | poolConn.returnConnection(conn); 86 | } catch (SQLException e) { 87 | e.printStackTrace(); 88 | } 89 | } 90 | 91 | if (request.getParameter("password") != null) { 92 | String sql = "update user set password=? where email=?"; 93 | String[] array = {request.getParameter("password"), Email}; 94 | int i = -1; 95 | try { 96 | i = poolConn.preparedStatement(sql, array); 97 | } catch (SQLException e) { 98 | e.printStackTrace(); 99 | } 100 | if (i != 1) { 101 | response.sendRedirect("user_error.html?Email=&name=&error=-1"); 102 | } 103 | } 104 | %> 105 |
106 | 邮箱:  <%=Email%> 107 |
108 | 109 |
110 |
    111 |
  1. 112 | 113 | 114 |
  2. 115 |
  3. 116 |
117 |
118 | 119 |
120 |
    121 |
  1. 122 | 123 | 127 |
  2. 128 |
  3. 129 | 130 | 133 |
  4. 134 |
  5. 135 |
136 |
137 | 138 | 139 | -------------------------------------------------------------------------------- /web/all_goods.jsp: -------------------------------------------------------------------------------- 1 | <%@ page import="db.PoolConn" %> 2 | <%@ page import="java.sql.Connection" %> 3 | <%@ page import="java.sql.PreparedStatement" %> 4 | <%@ page import="java.sql.ResultSet" %> 5 | <%@ page import="java.sql.SQLException" %><%-- 6 | Created by IntelliJ IDEA. 7 | User: 28713 8 | Date: 2016/12/20 9 | Time: 15:30 10 | To change this template use File | Settings | File Templates. 11 | --%> 12 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 13 | 14 | 15 | 16 | 17 | 所有商品 18 | 19 | 20 | 21 | 22 | 23 |
24 | <% 25 | session = request.getSession(); 26 | String Email = (String) session.getAttribute("Email"); 27 | if (Email == null) { 28 | %> 29 | 请登录  请注册 30 | <% 31 | } else { 32 | %> 33 | 欢迎您<%=Email%> 34 | 35 | <% 36 | } 37 | %> 38 |
39 | 48 |
49 | <% 50 | String sql = "SELECT count(*) FROM good"; 51 | String sql1 = "SELECT gid,gname,price FROM good LIMIT ?,?"; 52 | PoolConn poolConn = PoolConn.getPoolConn(); 53 | try (Connection conn = poolConn.getConnection(); 54 | PreparedStatement statement = conn.prepareStatement(sql); 55 | PreparedStatement statement1 = conn.prepareStatement(sql1); 56 | ResultSet resultSet = statement.executeQuery() 57 | ) { 58 | resultSet.next(); 59 | int TotalNum = resultSet.getInt(1); 60 | int pageSize = 15; 61 | String pageNo = request.getParameter("pageNo"); 62 | int curNo; 63 | if (pageNo == null) { 64 | curNo = 1; 65 | } else { 66 | curNo = Integer.parseInt(pageNo); 67 | } 68 | int minNum = (curNo - 1) * pageSize; 69 | int curNum = TotalNum - minNum; 70 | int TotalPageNo = TotalNum / pageSize + (TotalNum % pageSize > 0 ? 1 : 0); 71 | int x = 0; 72 | statement1.setInt(1, minNum); 73 | statement1.setInt(2, pageSize); 74 | ResultSet resultSet1 = statement1.executeQuery(); 75 | outLoop:for (int i = 0; i < 3; i++) { 76 | %> 77 | "); 96 | break outLoop; 97 | } 98 | } 99 | %> 100 | 101 | <% 102 | } 103 | %> 104 | 105 |
106 |
107 | <% 108 | final String ah = ""; 110 | final String ab = ""; 111 | String str = ah + curNo + ahb + curNo + ab; 112 | for (int i = 1; i <= 3; i++) { 113 | if (curNo - i > 1) 114 | str = ah + (curNo - i) + ahb + (curNo - i) + ab + " " + str; 115 | if (curNo + i < TotalPageNo) 116 | str = str + " " + ah + (curNo + i) + ahb + (curNo + i) + ab; 117 | } 118 | if (curNo - 4 > 1) 119 | str = "..." + " " + str; 120 | if (curNo > 1) 121 | str = ah + (curNo - 1) + ahb + "上一页 " + 122 | ah + 1 + ahb + 1 + ab + " " + str; 123 | if (curNo + 4 < TotalPageNo) 124 | str = str + " " + "..."; 125 | if (curNo < TotalPageNo) 126 | str = str + " " + ah + TotalPageNo + "\">" + TotalPageNo + ab + " " + 127 | ah + (curNo + 1) + ahb + "下一页" + ab; 128 | out.print(str); 129 | %> 130 |
131 | 142 | <% 143 | resultSet1.close(); 144 | poolConn.returnConnection(conn); 145 | } catch (SQLException e) { 146 | e.printStackTrace(); 147 | } 148 | %> 149 | 150 | 151 | -------------------------------------------------------------------------------- /web/my_cart.jsp: -------------------------------------------------------------------------------- 1 | <%@ page import="db.PoolConn" %> 2 | <%@ page import="java.sql.Connection" %> 3 | <%@ page import="java.sql.PreparedStatement" %> 4 | <%@ page import="java.sql.ResultSet" %> 5 | <%@ page import="java.sql.SQLException" %> 6 | <%@ page import="java.text.SimpleDateFormat" %> 7 | <%@ page import="java.util.Date" %><%-- 8 | Created by IntelliJ IDEA. 9 | User: 28713 10 | Date: 2016/12/27 11 | Time: 16:20 12 | To change this template use File | Settings | File Templates. 13 | --%> 14 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 15 | 16 | 17 | 我的购物车 18 | 19 | 20 | 21 | 22 |
23 | <% 24 | session = request.getSession(); 25 | String Email = (String) session.getAttribute("Email"); 26 | if (Email == null) { 27 | %> 28 | 29 | <% 30 | } else { 31 | %> 32 | 欢迎您<%=Email%> 33 | 34 |
35 | 44 | <% 45 | String sql = "SELECT cart.orderid,cart.gid,cart.number,good.gname,good.number,good.price FROM cart,good " + 46 | "WHERE cart.email=?&&cart.gid=good.gid"; 47 | PoolConn poolConn = PoolConn.getPoolConn(); 48 | try (Connection con = poolConn.getConnection(); 49 | PreparedStatement statement = con.prepareStatement(sql)) { 50 | statement.setString(1, Email); 51 | ResultSet resultSet = statement.executeQuery(); 52 | %> 53 |
54 | 55 | 56 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | <% 67 | SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分"); 68 | long orderId; 69 | Date date; 70 | long gid; 71 | String time; 72 | int i = 0; 73 | while (resultSet.next()) { 74 | orderId = resultSet.getLong(1); 75 | gid = resultSet.getLong(2); 76 | date = new Date(gid); 77 | time = formatter.format(date); 78 | %> 79 | 80 | 84 | 87 | 91 | 97 | 100 | 103 | 106 | 107 | <% 108 | i++; 109 | } 110 | } catch (SQLException e) { 111 | e.printStackTrace(); 112 | } 113 | } 114 | %> 115 |
57 | 58 | 59 | 加入时间商品名称购买数量单价
81 | 82 | 83 | 85 | <%=time%> 86 | 88 | <%=resultSet.getString(4)%> 89 | 90 | 92 | 93 | 96 | 98 | 单价:¥<%=resultSet.getString(6)%> 99 | 101 | 删除 102 | 104 |

105 |
116 | 117 | 118 | 119 |

120 |
121 | 132 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /src/db/ConnectionPool.java: -------------------------------------------------------------------------------- 1 | package db; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DatabaseMetaData; 5 | import java.sql.Driver; 6 | import java.sql.DriverManager; 7 | import java.sql.SQLException; 8 | import java.sql.Statement; 9 | import java.util.Enumeration; 10 | import java.util.Vector; 11 | 12 | /** 13 | * Created by 28713 on 2016/12/14. 14 | */ 15 | 16 | public class ConnectionPool { 17 | private String jdbcDriver = ""; // 数据库驱动 18 | private String dbUrl = ""; // 数据 URL 19 | private String dbUsername = ""; // 数据库用户名 20 | private String dbPassword = ""; // 数据库用户密码 21 | private String testTable = ""; // 测试连接是否可用的测试表名,默认没有测试表 22 | private int initialConnections = 5; // 连接池的初始大小 23 | private int incrementalConnections = 5; // 连接池自动增加的大小 24 | private int maxConnections = 50; // 连接池最大的大小 25 | private Vector connections = null; // 存放连接池中数据库连接的向量 , 初始时为 null 26 | // 它中存放的对象为 PooledConnection 型 27 | 28 | ConnectionPool(String jdbcDriver, String dbUrl, String dbUsername, 29 | String dbPassword) { 30 | this.jdbcDriver = jdbcDriver; 31 | this.dbUrl = dbUrl; 32 | this.dbUsername = dbUsername; 33 | this.dbPassword = dbPassword; 34 | } 35 | 36 | public int getInitialConnections() { 37 | return this.initialConnections; 38 | } 39 | 40 | public void setInitialConnections(int initialConnections) { 41 | this.initialConnections = initialConnections; 42 | } 43 | 44 | public int getIncrementalConnections() { 45 | return this.incrementalConnections; 46 | } 47 | 48 | public void setIncrementalConnections(int incrementalConnections) { 49 | this.incrementalConnections = incrementalConnections; 50 | } 51 | 52 | public int getMaxConnections() { 53 | return this.maxConnections; 54 | } 55 | 56 | public void setMaxConnections(int maxConnections) { 57 | this.maxConnections = maxConnections; 58 | } 59 | 60 | public String getTestTable() { 61 | return this.testTable; 62 | } 63 | 64 | public void setTestTable(String testTable) { 65 | this.testTable = testTable; 66 | } 67 | 68 | public synchronized void createPool() throws Exception { 69 | // 确保连接池没有创建 70 | // 假如连接池己经创建了,保存连接的向量 connections 不会为空 71 | if (connections != null) { 72 | return; // 假如己经创建,则返回 73 | } 74 | // 实例化 JDBC Driver 中指定的驱动类实例 75 | Driver driver = (Driver) (Class.forName(this.jdbcDriver).newInstance()); 76 | DriverManager.registerDriver(driver); // 注册 JDBC 驱动程序 77 | // 创建保存连接的向量 , 初始时有 0 个元素 78 | connections = new Vector(); 79 | // 根据 initialConnections 中设置的值,创建连接。 80 | createConnections(this.initialConnections); 81 | System.out.println("ConnectionPool creates successfully!"); 82 | } 83 | 84 | private void createConnections(int numConnections) throws SQLException { 85 | // 循环创建指定数目的数据库连接 86 | for (int x = 0; x < numConnections; x++) { 87 | // 是否连接池中的数据库连接的数量己经达到最大?最大值由类成员 maxConnections 88 | // 指出,假如 maxConnections 为 0 或负数,表示连接数量没有限制。 89 | // 假如连接数己经达到最大,即退出。 90 | if (this.maxConnections > 0 && 91 | this.connections.size() >= this.maxConnections) { 92 | break; 93 | } 94 | //add a new PooledConnection object to connections vector 95 | // 增加一个连接到连接池中(向量 connections 中) 96 | try { 97 | connections.addElement(new PooledConnection(newConnection())); 98 | } catch (SQLException e) { 99 | System.out.println("Fail to create ConnectionPool!" + e.getMessage()); 100 | throw new SQLException(); 101 | } 102 | System.out.println("Connection has be created!"); 103 | } 104 | } 105 | 106 | private Connection newConnection() throws SQLException { 107 | // 创建一个数据库连接 108 | Connection conn = DriverManager.getConnection(dbUrl, dbUsername, 109 | dbPassword); 110 | // 假如这是第一次创建数据库连接,即检查数据库,获得此数据库答应支持的 111 | // 最大客户连接数目 112 | //connections.size()==0 表示目前没有连接己被创建 113 | if (connections.size() == 0) { 114 | DatabaseMetaData metaData = conn.getMetaData(); 115 | int driverMaxConnections = metaData.getMaxConnections(); 116 | // 数据库返回的 driverMaxConnections 若为 0 ,表示此数据库没有最大 117 | // 连接限制,或数据库的最大连接限制不知道 118 | //driverMaxConnections 为返回的一个整数,表示此数据库答应客户连接的数目 119 | // 假如连接池中设置的最大连接数量大于数据库答应的连接数目 , 则置连接池的最大 120 | // 连接数目为数据库答应的最大数目 121 | if (driverMaxConnections > 0 && 122 | this.maxConnections > driverMaxConnections) { 123 | this.maxConnections = driverMaxConnections; 124 | } 125 | } 126 | return conn; // 返回创建的新的数据库连接 127 | } 128 | 129 | public synchronized Connection getConnection() throws SQLException { 130 | // 确保连接池己被创建 131 | if (connections == null) { 132 | return null; // 连接池还没创建,则返回 null 133 | } 134 | Connection conn = getFreeConnection(); // 获得一个可用的数据库连接 135 | // 假如目前没有可以使用的连接,即所有的连接都在使用中 136 | while (conn == null) { 137 | // 等一会再试 138 | wait(250); 139 | conn = getFreeConnection(); // 重新再试,直到获得可用的连接,假如 140 | //getFreeConnection() 返回的为 null 141 | // 则表明创建一批连接后也不可获得可用连接 142 | } 143 | return conn; // 返回获得的可用的连接 144 | } 145 | 146 | private Connection getFreeConnection() throws SQLException { 147 | // 从连接池中获得一个可用的数据库连接 148 | Connection conn = findFreeConnection(); 149 | if (conn == null) { 150 | // 假如目前连接池中没有可用的连接 151 | // 创建一些连接 152 | createConnections(incrementalConnections); 153 | // 重新从池中查找是否有可用连接 154 | conn = findFreeConnection(); 155 | if (conn == null) { 156 | // 假如创建连接后仍获得不到可用的连接,则返回 null 157 | return null; 158 | } 159 | } 160 | return conn; 161 | } 162 | 163 | private Connection findFreeConnection() throws SQLException { 164 | Connection conn = null; 165 | PooledConnection pConn = null; 166 | // 获得连接池向量中所有的对象 167 | Enumeration enumerate = connections.elements(); 168 | // 遍历所有的对象,看是否有可用的连接 169 | while (enumerate.hasMoreElements()) { 170 | pConn = (PooledConnection) enumerate.nextElement(); 171 | if (!pConn.isBusy()) { 172 | // 假如此对象不忙,则获得它的数据库连接并把它设为忙 173 | conn = pConn.getConnection(); 174 | pConn.setBusy(true); 175 | // 测试此连接是否可用 176 | if (!testConnection(conn)) { 177 | // 假如此连接不可再用了,则创建一个新的连接, 178 | // 并替换此不可用的连接对象,假如创建失败,返回 null 179 | try { 180 | conn = newConnection(); 181 | } catch (SQLException e) { 182 | System.out.println("Fail to create ConnectionPool!" + e.getMessage()); 183 | return null; 184 | } 185 | pConn.setConnection(conn); 186 | } 187 | break; // 己经找到一个可用的连接,退出 188 | } 189 | } 190 | return conn; // 返回找到到的可用连接 191 | } 192 | 193 | private boolean testConnection(Connection conn) { 194 | try { 195 | // 判定测试表是否存在 196 | if (testTable.equals("")) { 197 | // 假如测试表为空,试着使用此连接的 setAutoCommit() 方法 198 | // 来判定连接否可用(此方法只在部分数据库可用,假如不可用 , 199 | // 抛出异常)。注重:使用测试表的方法更可靠 200 | conn.setAutoCommit(true); 201 | } else { // 有测试表的时候使用测试表测试 202 | //check if this connection is valid 203 | Statement stmt = conn.createStatement(); 204 | stmt.execute("select count(*) from " + testTable); 205 | } 206 | } catch (SQLException e) { 207 | // 上面抛出异常,此连接己不可用,关闭它,并返回 false; 208 | closeConnection(conn); 209 | return false; 210 | } 211 | // 连接可用,返回 true 212 | return true; 213 | } 214 | 215 | public void returnConnection(Connection conn) { 216 | // 确保连接池存在,假如连接没有创建(不存在),直接返回 217 | if (connections == null) { 218 | System.out.println("ConnectionPool is not existed.Can't return!"); 219 | return; 220 | } 221 | PooledConnection pConn = null; 222 | Enumeration enumerate = connections.elements(); 223 | // 遍历连接池中的所有连接,找到这个要返回的连接对象 224 | while (enumerate.hasMoreElements()) { 225 | pConn = (PooledConnection) enumerate.nextElement(); 226 | // 先找到连接池中的要返回的连接对象 227 | if (conn == pConn.getConnection()) { 228 | // 找到了 , 设置此连接为空闲状态 229 | pConn.setBusy(false); 230 | break; 231 | } 232 | } 233 | } 234 | 235 | public synchronized void refreshConnections() throws SQLException { 236 | // 确保连接池己创新存在 237 | if (connections == null) { 238 | System.out.println("ConnectionPool is not existed.Can't refresh!"); 239 | return; 240 | } 241 | PooledConnection pConn = null; 242 | Enumeration enumerate = connections.elements(); 243 | while (enumerate.hasMoreElements()) { 244 | // 获得一个连接对象 245 | pConn = (PooledConnection) enumerate.nextElement(); 246 | // 假如对象忙则等 5 秒 ,5 秒后直接刷新 247 | if (pConn.isBusy()) { 248 | wait(5000); // 等 5 秒 249 | } 250 | // 关闭此连接,用一个新的连接代替它。 251 | closeConnection(pConn.getConnection()); 252 | pConn.setConnection(newConnection()); 253 | pConn.setBusy(false); 254 | } 255 | } 256 | 257 | public synchronized void closeConnectionPool() throws SQLException { 258 | // 确保连接池存在,假如不存在,返回 259 | if (connections == null) { 260 | System.out.println("ConnectionPool is not existed.Can't close!"); 261 | return; 262 | } 263 | PooledConnection pConn = null; 264 | Enumeration enumerate = connections.elements(); 265 | while (enumerate.hasMoreElements()) { 266 | pConn = (PooledConnection) enumerate.nextElement(); 267 | // 假如忙,等 5 秒 268 | if (pConn.isBusy()) { 269 | wait(5000); // 等 5 秒 270 | } 271 | //5 秒后直接关闭它 272 | closeConnection(pConn.getConnection()); 273 | // 从连接池向量中删除它 274 | connections.removeElement(pConn); 275 | } 276 | // 置连接池为空 277 | connections = null; 278 | } 279 | 280 | private void closeConnection(Connection conn) { 281 | try { 282 | conn.close(); 283 | } catch (SQLException e) { 284 | System.out.println("An error occur to close connection." + e.getMessage()); 285 | } 286 | } 287 | 288 | private void wait(int mSeconds) { 289 | try { 290 | Thread.sleep(mSeconds); 291 | } catch (InterruptedException e) { 292 | } 293 | } 294 | 295 | class PooledConnection { 296 | Connection connection = null; // 数据库连接 297 | boolean busy = false; // 此连接是否正在使用的标志,默认没有正在使用 298 | // 构造函数,根据一个 Connection 构告一个 PooledConnection 对象 299 | public PooledConnection(Connection connection) { 300 | this.connection = connection; 301 | } 302 | // 返回此对象中的连接 303 | public Connection getConnection() { 304 | return connection; 305 | } 306 | // 设置此对象的,连接 307 | public void setConnection(Connection connection) { 308 | this.connection = connection; 309 | } 310 | // 获得对象连接是否忙 311 | public boolean isBusy() { 312 | return busy; 313 | } 314 | // 设置对象的连接正在忙 315 | public void setBusy(boolean busy) { 316 | this.busy = busy; 317 | } 318 | } 319 | } 320 | --------------------------------------------------------------------------------