预备: 数据库 本次作业选择的数据库为MYSQL,采用的数据库管理工具为Navicat Premium12。MYSQL下载地址为https://dev.mysql.com/downloads/mysql/
使用Navicat连接数据库。
1.首先打开Navicat图形化管理界面,点击连接,若在本地开发,主机和端口没有占用的话直接默认的就可以。用户名和密码均为root,之后便可建立与数据库的链接,后面的数据库操作均可通过Navicat控制完成。
使用Navicat连接MYSQL数据库界面
2.设置Tomcat服务器,Java链接数据库最常用的就是通过jar包导入之后直接使用封装好的方法来实现的,安装JDBC驱动即可。本人使用的Tomcat版本为9.0.24,其中JDBC驱动为mysql-connector-java-5.1.48-bin.jar。将该jar包拷贝到服务器的lib文件夹中。重新启动服务器,能够成功启动即可。
3.最后通过eclipse建立JSP文件来测试能否实现数据库的连接。
测试内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page import ="java.sql.*" %> <html> <head> <title>页面输出数据库中制定数据表数据</title> </head> <body> <% try { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; Connection conn = DriverManager.getConnection(url, username, password); if (conn != null ){ out.print("数据库连接成功!" ); out.print("<br />" ); Statement stmt = null ; ResultSet rs = null ; String sql = "SELECT *FROM students;" ; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); out.print("查询结果:" ); out.print("<br />" ); out.println("姓名" +" " +"电话 " ); out.print("<br />" ); while (rs.next()) { out.println(rs.getString("name" )+"   " +rs.getString("tel" )+"   " ); out.print("<br />" ); } } else { out.print("连接失败!" ); } }catch (Exception e) { out.print("数据库连接异常!" ); } %> </body> </html>
使用Navicat看到的数据表中的数据
在网页端看到的数据表中的数据
二者内容一致说明数据库连接成功!
开始项目: 开发工具为Eclipse Java Photon。做好所有的预备工作后,即可开始开发。
开发时应注意代码的复用性以及可维护性。具体表现在:
1.所有代码分好类别存储,便于维护和修改。
2.涉及到数值问题的都用符号求得后表示,若绝对写死,一旦数据库中数据数量发生变化,项目在运行过程中容易出错。
3.数据库中数据表的设计应尽量减少冗余,在进行数据的操作时尽量使用多表查询,减轻服务器以及数据库的负担。
登录注册页的实现 需要注意的地方:
1.“永远不要相信用户的输入”,并非所有的用户输入都符合系统需求。在注册时添加确认密码一项使得用户能有缓冲时间。
2.输入时得情况判断:(1)用户是否输入完整。(2)输入完整时账号密码都正确、输入的账号不存在、输入的账号存在密码错误
需要注意的细节:在安全角度来讲,一旦用户输入密码错误不要单纯的给用户提示“密码错误”,最好提示“账号或密码错误”,在大型系统中防止黑客的暴力破解。
3.登录整体使用MVC的设计模式,通过JSP前端页面产生交互信息,交互信息与bean和servlet产生交互效果。
bean
useInfo.java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 package bean;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Connection;public class userInfo { String account; String password; public String getAccount () { return account; } public void setAccount (String account) { this .account = account; } public String getPassword () { return password; } public void setPassword (String password) { this .password = password; } public boolean storeMsg (userInfo u) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; Connection conn = DriverManager.getConnection(url, username, password); String acc = u.getAccount(); String pass = u.getPassword(); String sql_check = "select * from user where account=?" ; PreparedStatement ps_check = conn.prepareStatement(sql_check ); ps_check.setString(1 , acc); ResultSet rs = null ; rs = ps_check.executeQuery(); if (!rs.next()) { String sql = "insert into user(account,password) values (?,?)" ; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1 , acc); ps.setString(2 , pass); int result = 0 ; result = ps.executeUpdate(); ps.close(); conn.close(); if (result > 0 ) { return true ; } else { return false ; } }else { ps_check.close(); conn.close(); return false ; } } public boolean checkMsg (userInfo u) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; Connection conn = DriverManager.getConnection(url, username, password); ResultSet rs = null ; String acc = u.getAccount(); String sql = "select * from user where account=?" ; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1 , acc); rs = ps.executeQuery(); if (rs.next()) { u.setAccount(rs.getString(2 )); u.setPassword(rs.getString(3 )); ps.close(); conn.close(); return true ; } else { ps.close(); conn.close(); return false ; } } }
login.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> <link rel="stylesheet" href="css/login.css" > <link rel="icon" href="img/headicon.png" > </head> <body> <div class ="main" > <h1>登录</h1> <form action="login" method="post" > <p>账号</p> <input type="text" name="account" /> <p>密码</p> <input type="password" name="password" /> <input type="submit" value="提交" > <a href="register.jsp" > <input type="button" value="注册" > </a> </form> </div> <% request.setCharacterEncoding("utf-8" ); String account = request.getParameter("account" ); String password = request.getParameter("password" ); if (account == null ) { account = "" ; } if (password == null ) { password = "" ; } %> </body> </html>
login.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 package servlet;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import bean.userInfo;@WebServlet ("/login" )public class login extends HttpServlet { private static final long serialVersionUID = 1L ; public login () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); HttpSession session = request.getSession(); String account = request.getParameter("account" ); String password = request.getParameter("password" ); if (account.equals("" ) || password.equals("" )) { request.setCharacterEncoding("gb2312" ); session.setAttribute("msg" , "请将所有信息填写完整" ); response.sendRedirect("error/error.jsp" ); } else { userInfo u = new userInfo(); u.setAccount(account); u.setPassword(password); try { boolean data = u.checkMsg(u); if (data) { if (account.equals(u.getAccount()) && password.equals(u.getPassword())) { session.setAttribute("account" , account); response.sendRedirect("main/main.jsp" ); } else { session.setAttribute("msg" , "账号或密码错误,请重新输入" ); response.sendRedirect("error/error.jsp" ); } } else { session.setAttribute("msg" , "该账号不存在" ); response.sendRedirect("error/error.jsp" ); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
register.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册</title> <link rel="stylesheet" href="css/login.css" > <style> body { background: url("img/B3.jpg" ); background-size: cover; } </style> </head> <body> <div class ="main" > <h1>注册</h1> <form action="register" method="post" > <p>账号</p> <input type="text" name="account" /> <p>密码</p> <input type="password" name="password" /> <p>确认密码</p> <input type="password" name="checkPass" /> <input type="submit" value="确定" > </form> </div> <% request.setCharacterEncoding("utf-8" ); String account = request.getParameter("account" ); String password = request.getParameter("password" ); String checkPass = request.getParameter("checkPass" ); if (account == null || password == null || checkPass == null ) { account = "" ; password = "" ; checkPass = "" ; } %> </body> </html>
register.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 package servlet;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import bean.userInfo;@WebServlet ("/register" )public class register extends HttpServlet { private static final long serialVersionUID = 1L ; public register () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); request.setCharacterEncoding("utf-8" ); String account = request.getParameter("account" ); String password = request.getParameter("password" ); String checkPass = request.getParameter("checkPass" ); if (account.equals("" ) || password.equals("" ) || checkPass.equals("" )) { session.setAttribute("msg" , "请将所有信息输入完整_" ); response.sendRedirect("error/error.jsp" ); } else { if (!password.equals(checkPass)) { session.setAttribute("msg" , "两次输入的密码不一致,请重新输入_" ); response.sendRedirect("error/error.jsp" ); } else { userInfo u = new userInfo(); u.setAccount(account); u.setPassword(password); try { if (u.storeMsg(u)) { session.setAttribute("account" , account+"" ); session.setAttribute("password" , password+"" ); response.sendRedirect("success/regSuc.jsp" ); }else { session.setAttribute("msg" , "该账号已被注册" ); response.sendRedirect("error/error.jsp" ); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
页面截图:
登录页
注册页
### 主页面的实现
主页面中展示的为相关编程语言信息,里面的按钮为系统中最重要的可交互元素,其中数据全部来源于数据库,这样通过遍历在数据库中读数据可大大减少前端代码量,减轻前端开发时的负担。
main.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ page import ="java.sql.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8" > <title>主页</title> <link rel="stylesheet" href="../css/main.css" > <link rel="icon" href="../img/headicon.png" > </head> <body> <div class ="head" > <span>computer's language</span> <a href="../person/person.jsp"><span class="head_peo">欢迎你,<%=session.getAttribute("account")%></span></a> <a href="../person/mygoods.jsp"><span class="head_peo">个人购物车</span></a> </div> <div class="container"> <% try { Class.forName("com.mysql.jdbc.Driver"); //驱动程序名 String url = "jdbc:mysql://localhost:3306/test"; //数据库名 String username = "root"; //数据库用户名 String password = "root"; //数据库用户密码 Connection conn = DriverManager.getConnection(url, username, password); //连接状态 if (conn != null) { Statement stmt = null; ResultSet rs = null; String sql = "SELECT *FROM content;"; //查询语句 stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int all = 0; while (rs.next()) { all++; session.setAttribute("num", all + ""); %> <div class="item"> <div class="img"> <img src=<%=rs.getString("img")%> alt="python"> </div> <div class="font"> <p><%=rs.getString("language")%></p> <p class="describe"><%=rs.getString("des")%></p> </div> <div class="button"> <!-- 通过点击不同的按钮跳到指定相关的购物界面 --> <form action="../goshop" method="post"> <input type="submit" name=<%=rs.getString("id") + "_learn"%> value="学习" class="more"> </form> <form action="../getContent" method="post"> <input type="submit" name=<%=rs.getString("id") + "_like"%> value="喜欢" class="like"> </form> <a href=<%=rs.getString("url")%> target="_blank"> <button class="official">官网</button> </a> </div> </div> <% } } else { out.print("连接失败!"); } } catch (Exception e) { out.print("数据库连接异常!"); } %> </div> </body> </html>
goshop.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 package servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebServlet ("/goshop" )public class goshop extends HttpServlet { private static final long serialVersionUID = 1L ; public goshop () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); response.setCharacterEncoding("utf-8" ); response.setContentType("text/html;chartset=utf-8" ); HttpSession session = request.getSession(); String n = (String) session.getAttribute("num" ); int num = Integer.parseInt(n); for (int i = 0 ; i < num; i++) { if (request.getParameter(i + "_learn" ) != null ) { session.setAttribute("learn" , i + "" ); response.sendRedirect("shop/shop.jsp" ); } } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
getContent.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebServlet ("/getContent" )public class getContent extends HttpServlet { private static final long serialVersionUID = 1L ; public getContent () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); response.setCharacterEncoding("utf-8" ); response.setContentType("text/html;chartset=utf-8" ); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); try { Class.forName("com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; try { Connection conn = DriverManager.getConnection(url, username, password); Statement stmt = null ; ResultSet rs = null ; String sql = "insert into user_like(account,language_id) values (?,?)" ; String acc = (String) session.getAttribute("account" ); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1 , acc); String n = (String) session.getAttribute("num" ); int num = Integer.parseInt(n); int result = 0 ; for (int i = 0 ; i < num; i++) { if (request.getParameter(i + "_like" ) != null ) { String test_sql = "select * from user_like where account=" +acc+" and language_id=" +i; stmt = conn.createStatement(); rs = stmt.executeQuery(test_sql); if (rs.next()) { session.setAttribute("msg" , "你已点击了喜欢" ); response.sendRedirect("error/showMsg.jsp" ); } else { ps.setInt(2 , i); result = ps.executeUpdate(); ps.close(); conn.close(); if (result > 0 ) { out.print("插入成功" ); out.print(num); result = 0 ; response.sendRedirect("success/insertSuc.jsp" ); } else { out.print("插入失败" ); } } } } } catch (SQLException e) { e.printStackTrace(); } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
主页面截图:
主页
个人页面的实现 个人页面中主要展示用户喜欢的编程语言,在这个页面可进行已保存数据的删除,数据全部来自数据库。
该页中的用户信息来自session的存储,(这种方式并不安全,如果浏览器禁掉cookie使用,那么将无法产生该有的作用,我这只是为了使得项目中能够使用到session知识点)
person.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ page import ="java.sql.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8" > <title>个人页面</title> <link rel="stylesheet" href="../css/person.css" > </head> <body> <div class ="head" > <span>个人页面</span> <a href="../main/main.jsp"><span class="head_peo">主页面</span></a> </div> <div class ="container" > <% session.getAttribute("account" ); try { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; Connection conn = DriverManager.getConnection(url, username, password); if (conn != null ) { Statement stmt = null ; ResultSet rs = null ; String sql = "SELECT user_like.id, language,language_id,des,url,img FROM user_like,content where user_like.language_id=content.id and account=" + (String) session.getAttribute("account" ); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { %> <div class ="item" > <div class ="img" > <img src=<%=rs.getString("img" )%>> </div> <div class ="font" > <p><%=rs.getString("language")%></p> </div> <div class ="button" > <form action="../deleteLike" method="post" > <input type="submit" name=<%=rs.getString("language_id" ) + "_unlike" %> value="取消喜欢" class ="like" > </form> <a href=<%=rs.getString("url" )%> target="_blank" > <button class="official">官网</button> </a> </div> </div> <% } } else { out.print("连接失败!" ); } } catch (Exception e) { out.print("数据库连接异常!" ); } %> </div> </body> </html>
deleteLike.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebServlet ("/deleteLike" )public class deleteLike extends HttpServlet { private static final long serialVersionUID = 1L ; public deleteLike () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); response.setCharacterEncoding("utf-8" ); response.setContentType("text/html;chartset=utf-8" ); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); try { Class.forName("com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; try { Connection conn = DriverManager.getConnection(url, username, password); String sql = "delete from user_like where account=? and language_id=?" ; String acc = (String) session.getAttribute("account" ); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1 , acc); String n = (String) session.getAttribute("num" ); int num = Integer.parseInt(n); int result = 0 ; for (int i = 0 ; i < num; i++) { if (request.getParameter(i + "_unlike" ) != null ) { ps.setInt(2 , i); result = ps.executeUpdate(); ps.close(); conn.close(); if (result > 0 ) { result = 0 ; response.sendRedirect("success/deleteSuc.jsp" ); } else { out.print("删除失败" ); } } } } catch (SQLException e) { e.printStackTrace(); } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
个人页面截图:
个人页面
学习资料页面的实现 用户在主页面点击不同语言的学习按钮,会显示不同的学习资料,主要使用SQL查询来实现,通过唯一的主键来确定。并且支持学习资料的购买个数选择。
shop.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page import ="java.sql.*" %> <html> <head> <title>学习资料</title> <link rel="stylesheet" href="../css/shop.css" > </head> <body> <div class ="head" > <span>学习资料</span> <a href="../main/main.jsp"><span class="head_peo">回到主页</span></a> </div> <div class ="container" > <% try { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; Connection conn = DriverManager.getConnection(url, username, password); if (conn != null ) { Statement stmt = null ; ResultSet rs = null ; String sql = "SELECT id,bprice, bdes,img,lid FROM goods where lid in (select id from content where id=" + (String) session.getAttribute("learn" ) + ")" ; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { %> <div class ="item" > <img src=<%=rs.getString("img" )%> alt="" > <hr style="height: 2px; border: none; border-top: 5px ridge black;" /> <div class ="price" > ¥<%=rs.getFloat("bprice" )%> </div> <div class ="describe" > <%=rs.getString("bdes" )%> </div> <div class ="operation" > <form action="../buyGoods" method="post" > <span>购买数量:</span> <select name="buyNum" > <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="购买" name=<%=rs.getString("id" ) +"_buy" %>> </form> </div> </div> <% } } else { out.print("连接失败!" ); } } catch (Exception e) { out.print("数据库连接异常!" ); } %> </div> </body> </html>
buyGoods.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebServlet ("/buyGoods" )public class buyGoods extends HttpServlet { private static final long serialVersionUID = 1L ; public buyGoods () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); response.setCharacterEncoding("utf-8" ); response.setContentType("text/html;chartset=utf-8" ); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); try { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; Connection conn = DriverManager.getConnection(url, username, password); if (conn != null ) { Statement stmt = null ; ResultSet rs = null ; String sql = "SELECT * FROM goods;" ; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); int count = 0 ; while (rs.next()) { count++; } out.print(count); for (int i = 0 ; i < count; i++) { if (request.getParameter(i + "_buy" ) != null ) { String acc = (String) session.getAttribute("account" ); String num = request.getParameter("buyNum" ); int number=Integer.parseInt(num); int result=0 ; String sql_buy = "insert into buy(account,gid,num) values (?,?,?)" ; PreparedStatement ps = conn.prepareStatement(sql_buy); ps.setString(1 , acc); ps.setInt(2 , i); ps.setInt(3 , number); result = ps.executeUpdate(); ps.close(); conn.close(); if (result > 0 ) { out.print("插入成功" ); out.print(num); result = 0 ; response.sendRedirect("success/insertSuc1.jsp" ); } else { out.print("插入失败" ); } } } } else { out.print("连接失败!" ); } } catch (Exception e) { out.print("数据库连接异常!" ); } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
相关截图:
学习资料页
购买成功
购物车页面的实现 购物车中主要为用户添加的相关学习资料信息,在这个页面用户可取消相关资料的购买,同时也可以对已经加入购物车的数据进行数量的修改,之后页面中重新显示所有的价钱。
mygoods.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ page import ="java.sql.*" %> <%@ page import ="java.util.ArrayList" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8" > <title>购物车</title> <link rel="stylesheet" href="../css/shop.css" > </head> <% float money = 0 ; %> <body> <div class ="head" style="margin-bottom: 0" > <span>购物车</span> <a href="../main/main.jsp"><span class="head_peo">主页面</span></a> </div> <div class ="container" > <% session.getAttribute("account" ); try { Class.forName("com.mysql.jdbc.Driver" ); String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; Connection conn = DriverManager.getConnection(url, username, password); if (conn != null ) { Statement stmt = null ; ResultSet rs = null ; String sql = "select * from goods,buy where buy.gid=goods.id and account=" + (String) session.getAttribute("account" ); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); ArrayList<Integer> List = new ArrayList<Integer>(); while (rs.next()) { money += rs.getInt("num" ) * rs.getFloat("bprice" ); List.add(rs.getInt("buy.id" )); session.setAttribute("list" , List); %> <div class ="item" style="margin-top: 40px;height:400px;" > <img src=<%=rs.getString("img" )%> alt="" > <hr style="height: 2px; border: none; border-top: 5px ridge black;" /> <div class ="price" > ¥<%=rs.getFloat("bprice" )%> </div> <div class ="describe" > <%=rs.getString("bdes" )%> </div> <div class ="operation" > <form action="../deleteBuy" method="post" > <span>购买数量:<%=rs.getInt("num")%></span> <input type="submit" value="取消购买" name=<%=rs.getInt("buy.id" ) + "_notBuy" %>> </form> </div> <hr style="height: 2px; border: none; border-top: 2px ridge black;margin-top:20px;" /> <div class ="operation" > <form action="../changeBuyNum" method="post" > <span>修改购买数量:</span> <select name="buyNum" > <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="确定" name=<%=rs.getInt("buy.id" ) + "_changeBuy" %> style="background:green;" > </form> </div> </div> <% } } else { out.print("连接失败!" ); } } catch (Exception e) { out.print("数据库连接异常!" ); } %> </div> <div class ="all" > 一共<%=money%>元 </div> </body> </html>
deleteBuy.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebServlet ("/deleteBuy" )public class deleteBuy extends HttpServlet { private static final long serialVersionUID = 1L ; public deleteBuy () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); response.setCharacterEncoding("utf-8" ); response.setContentType("text/html;chartset=utf-8" ); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); try { Class.forName("com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; try { Connection conn = DriverManager.getConnection(url, username, password); String sql = "delete from buy where id=?" ; PreparedStatement ps = conn.prepareStatement(sql); ArrayList<Integer> num = (ArrayList<Integer>) session.getAttribute("list" ); int result = 0 ; out.print(num); for (int i = 0 ; i < num.size(); i++) { if (request.getParameter(num.get(i) + "_notBuy" ) != null ) { ps.setInt(1 , num.get(i)); result = ps.executeUpdate(); ps.close(); conn.close(); if (result > 0 ) { out.print("删除" ); out.print(num); result = 0 ; response.sendRedirect("success/notBuySuc.jsp" ); } else { out.print("删除失败" ); } } } } catch (SQLException e) { e.printStackTrace(); } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
changeBuyNum.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebServlet ("/changeBuyNum" )public class changeBuyNum extends HttpServlet { private static final long serialVersionUID = 1L ; public changeBuyNum () { super (); } protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8" ); response.setCharacterEncoding("utf-8" ); response.setContentType("text/html;chartset=utf-8" ); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); try { Class.forName("com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/test" ; String username = "root" ; String password = "root" ; try { Connection conn = DriverManager.getConnection(url, username, password); String sql = "update buy set num=? where id=?" ; PreparedStatement ps = conn.prepareStatement(sql); ArrayList<Integer> num = (ArrayList<Integer>) session.getAttribute("list" ); int result = 0 ; out.print(num); for (int i = 0 ; i < num.size(); i++) { if (request.getParameter(num.get(i) + "_changeBuy" ) != null ) { String number = request.getParameter("buyNum" ); int sum = Integer.parseInt(number); ps.setInt(1 , sum); ps.setInt(2 , num.get(i)); result = ps.executeUpdate(); ps.close(); conn.close(); if (result > 0 ) { out.print("修改" ); out.print(num); result = 0 ; response.sendRedirect("success/changeNumSuc.jsp" ); } else { out.print("修改失败" ); } } } } catch (SQLException e) { e.printStackTrace(); } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
相关截图:
购物车
数据库表的设计:
设计理念为尽可能地减少数据表地冗余性,增加相关性