SRemoveGoodServlet
package org.example.controllor; import org.apache.catalina.session.StandardSession; import org.example.api.IGoodService; import org.example.service.impl.GoodServiceImpl; import org.example.api.entity.Good; import org.example.api.entity.GoodList; import org.example.api.utils.JavaDateConnectionUtils; import org.example.service.impl.GoodServiceImpl; 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 java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @WebServlet("/SRemoveGood") public class SRemoveGoodServlet extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 防止乱码 response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); String userName = request.getParameter("userName"); IGoodService iGoodService = new GoodServiceImpl(); GoodList goodList = new GoodList(); boolean judgment = false; try { judgment = iGoodService.select(id); } catch (SQLException throwables) { throwables.printStackTrace(); } if (judgment == true && userName.equals("侯凯")) { JavaDateConnectionUtils javaDateConnection = new JavaDateConnectionUtils(); PreparedStatement preparedStatement = null; Connection connection = null; try { connection = javaDateConnection.getConn(); String sql = "delete from t_good where goodId=?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, id); int rs = preparedStatement.executeUpdate(); if (rs == 1) { HttpSession httpSession = request.getSession(); request.setAttribute("ji","商品删除成功!!!"); request.getRequestDispatcher("showgoods.jsp").forward(request, response); } else { request.getRequestDispatcher("showgoods.jsp").forward(request,response); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { try { connection.close(); preparedStatement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } else { HttpSession httpSession = request.getSession(); request.setAttribute("ji","商品删除失败!!!您无删除商品的权限,"); request.getRequestDispatcher("showgoods.jsp").forward(request,response); } } }
# web层 ```java ```java ```java ```clike <%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>账号注册</title> </head> <style> #a { width:50%; height:50%; border: 1px dashed ; background-color: #90eee3; text-align:center; } body{ background-color: #e0f9ff; } </style> <body> <div id="a"> <h1>注销账号</h1> <form action="/DeleteUser" method="post"> 用 户 名: <input type="text" name="userName"> <br> 密 码: <input type="password" name="userPassword"> <br> 手 机 号: <input type="text" name="telephone"> <br> <input type="submit" value="注销"> </form> </div> <h3 align="center" ><%=request.getAttribute("msg")%></h3> <h2 align="center"><a href="register.jsp">点击重新注册账号</a></h2> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="UTF-8"> <title>黑市商城首页</title> </head> <%response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8");%> <body bgcolor="#add8e6"> <table width="100%"> <tr> <td> <table width="100%"> <tr> <td> <a href="register.jsp">注册</a> <a href="login.jsp">登录</a> <a href="delete.jsp">注销</a> <a href="showcar.jsp">订单</a> <a href="#">帮助</a> </td> </tr> </table> </td> </tr> <!--第二部分--> <tr bgcolor="black"> <td height="50px"> <a href="#"><font color="white">首页</font></a> <a href="#"><font color="white">碳酸饮料</font></a> <a href="#"><font color="white">乳饮</font></a> <a href="#"><font color="white">果茶</font></a> <a href="#"><font color="white">其他饮料</font></a> </td> </tr> <tr> <td> <img src="https://n.sinaimg.cn/spider2020420/133/w600h333/20200420/4c7d-iskepxt7981236.png" height="300px" width="100%" /> </td> </tr> <tr> <td> <table> <tr> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> </tr> <tr> <td> <img src="https://cbu01.alicdn.com/img/ibank/2019/935/265/11703562539_780901980.jpg" height="200px" width="200px"/> <p><a>旺仔牛奶(一排)</a></p> <p><font color="red">$20</font></p> </td> <td> <img src="https://tse3-mm.cn.bing.net/th/id/OIP.kz-2OVuNiFTQrYHAD7jP2AAAAA?pid=ImgDet&rs=1" height="200px" width="200px"/> <p><a>脉动</a></p> <p><font color="red">$5</font></p> </td> <td> <img src="https://th.bing.com/th/id/R48187afbfb548050648990f0dd7a3fe0?rik=cXl%2bo9kjuPe5YA&riu=http%3a%2f%2fwww.shikefood.com%2fimages%2f201706%2fsource_img%2f10502_P_1497391133066.jpg&ehk=4xKqaMC%2fueYJILKz3HynqZJ25tDpC2%2bZf5YKEAkPfTA%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>乌龙茶</a></p> <p><font color="red">$1</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rdb7c7cba11e8e880930a480ff0db0c87?rik=cTjPOJPJyoleSA&riu=http%3a%2f%2fpic109.nipic.com%2ffile%2f20160916%2f6062299_163706413000_2.jpg&ehk=1nPt6VryGF2MlNMFi8ChxJ1PhlVp1k1%2fKnz2bLkL6VQ%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>可口可乐</a></p> <p><font color="red">$3</font></p> </td> </tr> <tr> <td> <img src="https://th.bing.com/th/id/Rbf660f71a587e9c365139518e7b084b1?rik=O1SdZo5%2f8tmEpQ&riu=http%3a%2f%2fpic299.nipic.com%2ffile%2f20200611%2f19051700_194646446000_2.jpg&ehk=GDt8l%2bRaESjBBHz%2bfP5TVTqjtCGNHypsIubHbRgbsSs%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>果茶</a></p> <p><font color="red">$15</font></p> </td> <td> <img src="https://tse1-mm.cn.bing.net/th/id/OIP.WzXja99RFLlHs4LreoO3GQHaGq?pid=ImgDet&rs=1" height="200px" width="200px"/> <p><a>七喜</a></p> <p><font color="red">$7</font></p> </td> <td> <img src="https://imgservice4.suning.cn/uimg1/b2c/image/gs0RyM76yxGFGVefFTHpAg==.jpg_400w_400h_4e" height="200px" width="200px"/> <p><a>水晶葡萄</a></p> <p><font color="red">$4.5</font></p> </td> <td> <img src="https://tse3-mm.cn.bing.net/th/id/OIP.U6N6vZvwHFWmqFq8Pk8tPQHaKE?pid=ImgDet&rs=1" height="200px" width="200px"/> <p><a>美年达</a></p> <p><font color="red">$3</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rc68719c95e68240324bad6459d625675?rik=1SpDWN3ULeZtsQ&riu=http%3a%2f%2fimages.zon100.com%2fimg%2f10000518%2f1100141218%2f1100206232_1000.jpg&ehk=DcWYen6bIosj%2f4LJlX0g1xbitDegc7ga78tDMlubvo4%3d&risl=&pid=ImgRaw" height="200px" width="200px"/> <p><a>绿茶</a></p> <p><font color="red">$5</font></p> </td> <td> <img src="https://th.bing.com/th/id/Rbc672bbf2693119837a8d3f5df6e2fbd?rik=KpfIwTaSBYPgdw&riu=http%3a%2f%2fwx1.sinaimg.cn%2flarge%2f006APoFYly1ga7ynozy6eg308c069dg7.gif&ehk=raDwaMUtQHU1LIZCkiu2tn91MXGEsd1k5uqrK7hXh7I%3d&risl=&pid=ImgRaw" height="200px" width="220px"/> <p><a>营养快线</a></p> <p><font color="red">$998</font></p> </td> </tr> </table> </td> </tr> <tr> <td> <img src="picture/banner.jpg" height="5%" width="100%"/> </td> </tr> <tr> <td align="center"> <a href="#">服务保障</a> <a href="#">联系我们</a> <a href="#">关于我们</a> <a href="#">投诉建议</a> <br /> <p>HK黑市商城©2021-4.18火爆开启</p> </td> </tr> </table> </body> </html> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户登录</title> <style> #a { width:50%; height:200px; border: 1px dashed ; background-color: #e0fff4; text-align:center; } body{ background-color:lightblue; } </style> </head> <body> <div id="a"> <h1>登录界面</h1> <form action="/loginUser" method="post"> 账号:<input type="text" name="userName"/> <br> 密码:<input type="password" name="userPassword"/> <br> <input type="submit" value="登录"/> 没有账号?<a href ="register.jsp">注册账号</a><br> 多余账号?<a href="delete.jsp">注销账号</a> </form> </div> <h3 align="center" ><%=request.getAttribute("msg")%></h3> <h2 align="center"><a href="showgoods.jsp">点击进入商品界面</a></h2> </body> </html> <%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>账号注册</title> </head> <style> #a { width:50%; height:50%; border: 1px dashed ; background-color: #90eee6; text-align:center; } body{ background-color: #e0f9ff; } </style> <body> <div id="a"> <h1>注册账号</h1> <form action="/InsertUser" method="post"> 用 户 名: <input type="text" name="userName"> <br> 密 码: <input type="password" name="userPassword"> <br> 性 别: <input type="text" name="sex"> <br> 年 龄: <input type="text" name="age"> <br> 手 机 号: <input type="text" name="telephone"> <br> <input type="submit" value="注册"> 已有账号?<a href ="login.jsp">点击登录</a><br> </form> </div> <h3 align="center" ><%=request.getAttribute("msg")%></h3> </body> </html> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>添加商品</title> <style> #a { width:50%; height:200px; border: 1px dashed ; background-color: #e0fff4; text-align:center; } <% String userName = request.getParameter("userName"); response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); %> body{ background-color:lightblue; } </style> </head> <body> <div id="a"> <h1>添加商品界面</h1> <form action="/SAddGood?userName=<%=userName%>" method="post"> 商品名称:<input type="text" name="goodName"/> <br> 商品价格:<input type="text" name="goodValue"/> <br> 商品ID:<input type="text" name="goodId"/> <br> <input type="submit" value="添加"/> </form> </div> <h3 align="center" ><%=request.getAttribute("msg")%></h3> <h2 align="center"><a href="showgoods.jsp">点击进入商品界面</a></h2> </body> </html> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="org.example.api.entity.Good" %> <%@ page import="org.example.api.entity.GoodList" %> <%@ page import="org.example.api.utils.MyTools" %> <%@ page import="org.example.api.entity.OrderList" %> <% float total = 0; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="UTF-8"> <link href="styles.css" type="text/css" rel="stylesheet" /> <title>黑市订单</title> </head> <body> <% response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); HttpSession httpSession = request.getSession(); String userName = (String) httpSession.getAttribute("userName"); %> <h3 align="left"><a href="/SelectOrder?userName=<%=userName%>">点击刷新订单</a></h3> <h3 align="right"><a href="index.jsp">返回主页面</a></h3> <% ArrayList <Good> buyList = null; buyList = (ArrayList<Good>) request.getAttribute("Array1"); %> <table> <tr height="50"> <td colspan="3" align="center" ><h1> 订单</h1></td> </tr> <tr align="center" height="30" bgcolor="lightgrey"> <td>名称</td> <td>价格</td> <td>数量</td> <td>移除</td> </tr> <% if(buyList == null || buyList.size() == 0){ %> <tr height="100"> <td colspan="3" align="center">您的订单还未添加任何商品!<br> 请尝试刷新您的订单!</td> </tr> <% }else{ for(int i = 0; i < buyList.size(); i++){ Good single = buyList.get(i); float money = MyTools.strToInt(single.getGoodValue())*MyTools.strToInt(single.getGoodNum()); total += money; %> <tr align="center" height="50"> <td><%= single.getGoodName() %></td> <td><%= single.getGoodValue() %></td> <td> <a href="/AddGood?id=<%=single.getGoodId()%>&userName=<%=userName%>"> <img src="https://img.51miz.com/Element/00/79/78/04/372c7712_E797804_81ee5239.png" width="20" height="20"/> </a> <strong><%=single.getGoodNum()%></strong> <a href="/RemoveGood?id=<%=single.getGoodId()%>&userName=<%=userName%>"> <img src="https://th.bing.com/th/id/Rcde7b52d6b859e47df4f30a4980b9463?rik=AcAi8g%2bYJfQoDA&riu=http%3a%2f%2fbpic.588ku.com%2felement_pic%2f01%2f35%2f82%2f18573bf7012f19d.jpg&ehk=VxpGi3%2fpJexj4F4DOLo6a5gL1h6XaX%2b%2b51f6TPL7iGI%3d&risl=&pid=ImgRaw" width="20" height="20"/> </a> </td> <td><a href="/RemoveOrder?id=<%=single.getGoodId()%>&userName=<%=userName%>">移除</a></td> </tr> <% } } %> <tr height="50"> <td colspan="3" align="left" ><h4>应付金额:<%= total %></h4></td> <br> </tr> <tr align="center" height="50"> <td align="left" colspan="3"><a href="showgoods.jsp">继续购物</a></td> <td align="right" colspan="3"><a href="/RemoveAllGood?userName=<%=userName%>">清除订单</a></td> </tr> </table> </body> </html> <%@page import="java.util.ArrayList"%> <%@page import="org.example.controllor.SelectGoodServlet" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="org.example.api.entity.Good" %> <%@ page import="org.example.api.entity.GoodList" %> <%@ page import="java.security.Principal" %> <jsp:useBean id="goodList" class="org.example.api.entity.GoodList" scope="session"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>黑市</title> <link href="styles.css" type="text/css" rel="stylesheet" /> </head> <body> <% response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); %> <% HttpSession httpSession = request.getSession(); ArrayList <Good> goodsList = new ArrayList<Good>(); String userName = (String) httpSession.getAttribute("userName"); goodsList = (ArrayList<Good>) httpSession.getAttribute("Array"); String ji = request.getAttribute("ji") + "请重新刷新商品页面以查看全部商品"; %> <a href="/SelectGood">点击刷新商品</a> <script>alert("<%=ji%>")</script> <table> <tr height="50"> <td colspan="3" align="center" ><h1> 黑市商城</h1></td> </tr> <tr align="center" height="30" bgcolor="lightgrey"> <td></td> <td>名称</td> <td>价格</td> <td>购买</td> </tr> <% if(goodsList == null || goodsList.size() == 0){ %> <tr height="100"> <td colspan="3" align="center">没有商品可以显示!<br> 请尝试刷新商品!</td> </tr> <% }else{ for(int i = 0; i < goodsList.size(); i++){ Good single =goodsList.get(i); %> <tr align="center" height="50"> <td> <a href="/SRemoveGood?userName=<%=userName%>&id=<%=single.getGoodId()%>"> <img src="https://img.51miz.com/Element/00/77/34/97/c78a8ea1_E773497_8e853fd8.png" width="30" height="30"/> </a> </td> <td><%= single.getGoodName() %></td> <td><%= single.getGoodValue() %></td> <td> <a href="/InsertGood?id=<%=single.getGoodId()%>&name=<%=single.getGoodName()%>&value=<%=single.getGoodValue()%>&userName=<%=userName%>">购买</a> </td> </tr> <% } } %> <tr align="center" height="50"> <td align="left" colspan="3"> <a href="/SAddGood.jsp?userName=<%=userName%>"> 添加商品 </a> </td> <td align="right" colspan="3"><a href="showcar.jsp">查看订单</a></td> </tr> </table> </body> </html> @charset "UTF-8"; table{ border: 1px; width: 500px; rules: none; cellspacing:0px; cellpadding:0px; } tr{ height: 50px; } td{ width:100px; } a:link { text-decoration: none; } a:visited { text-decoration: none; } a:hover { text-decoration: none; } a:active { text-decoration: none; }
dao层
selectGood
```clike package org.example.dao; import java.sql.SQLException; public interface IGoodDao { /** * 查询全部商品 * @return * @throws SQLException */ boolean selectGood() throws SQLException; /** * 根据商品id查询特定商品 * @param id * @return * @throws SQLException */ boolean select(String id) throws SQLException; } package org.example.dao; import java.sql.SQLException; public interface IOrderDao { /** * 根据订单id及所传的userName查询订单 * @param orderId * @param userName * @return * @throws SQLException */ boolean selectOrder(String orderId , String userName) throws SQLException; /** * 查询全部订单 * @return * @throws SQLException */ boolean select() throws SQLException; } package org.example.dao; import java.sql.SQLException; public interface IUserDao { /** * 根据用户账号查询用户 * @param userName * @return * @throws SQLException */ boolean loginUser(String userName) throws SQLException; /** * 根据用户名及密码查询特定用户 * @param userName * @param userPassword * @return * @throws SQLException */ boolean loginUser1(String userName,String userPassword)throws SQLException; }
selectGood
package org.example.dao.impl; import org.example.dao.IUserDao; import org.example.api.utils.JavaDateConnectionUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class selectGood implements IUserDao { int a=0; JavaDateConnectionUtils javaDateConnectionUtils = new JavaDateConnectionUtils(); Connection connection = javaDateConnectionUtils.getConn(); @Override public boolean loginUser(String userName) throws SQLException { String iSql = "select * from t_user where userName=?"; PreparedStatement preparedStatement = null; preparedStatement = connection.prepareStatement(iSql); preparedStatement.setString(1,userName); ResultSet re= preparedStatement.executeQuery(); while (re.next()){ a++; System.out.println(re.getString("userName")+""+re.getString("userPassword")); }if (a!=0){ System.out.println("查询成功"); preparedStatement.close(); connection.close(); return true;} else {preparedStatement.close(); connection.close(); System.out.println("查询失败"); return false; } } @Override public boolean loginUser1(String userName, String userPassword) throws SQLException { String iSql = "select * from t_user where userName=? and userPassword=?"; PreparedStatement preparedStatement = null; preparedStatement = connection.prepareStatement(iSql); preparedStatement.setString(1,userName); preparedStatement.setString(2,userPassword); ResultSet re= preparedStatement.executeQuery(); while (re.next()){ a++; System.out.println(re.getString("userName")+""+re.getString("userPassword")); }if (a!=0){ System.out.println("查询成功"); preparedStatement.close(); connection.close(); return true;} else {preparedStatement.close(); connection.close(); System.out.println("查询失败"); return false; } } }
impl.OrderDaoImpl
package org.example.dao.impl; import org.example.dao.IOrderDao; import org.example.api.utils.JavaDateConnectionUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class OrderDaoImpl implements IOrderDao { int a = 0; JavaDateConnectionUtils javaDateConnectionUtils = new JavaDateConnectionUtils(); Connection connection = javaDateConnectionUtils.getConn(); @Override public boolean selectOrder(String orderId , String userName) throws SQLException { String iSql = "select * from t_order where orderId=? and userName=?"; PreparedStatement preparedStatement = null; preparedStatement = connection.prepareStatement(iSql); preparedStatement.setString(1, orderId); preparedStatement.setString(2,userName); ResultSet re = preparedStatement.executeQuery(); while (re.next()) { a++; System.out.println(re.getString("orderId")+re.getString("userName")); } if (a != 0) { System.out.println("查询成功"); preparedStatement.close(); connection.close(); return true; } else { preparedStatement.close(); connection.close(); System.out.println("查询失败"); return false; } } public boolean select () throws SQLException { String iSql = "select * from t_order "; PreparedStatement preparedStatement = null; preparedStatement = connection.prepareStatement(iSql); ResultSet re = preparedStatement.executeQuery(); while (re.next()) { a++; System.out.println(re.getString("orderId")); } if (a != 0) { System.out.println("查询成功"); preparedStatement.close(); connection.close(); return true; } else { preparedStatement.close(); connection.close(); System.out.println("查询失败"); return false; } } }
impl.GoodDaoImpl
package org.example.dao.impl; import org.example.api.utils.JavaDateConnectionUtils; import org.example.dao.IGoodDao; import org.example.api.utils.JavaDateConnectionUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class GoodDaoImpl implements IGoodDao { int a=0; JavaDateConnectionUtils javaDateConnectionUtils = new JavaDateConnectionUtils(); Connection connection = javaDateConnectionUtils.getConn(); @Override public boolean selectGood() throws SQLException { String iSql = "select * from t_good "; PreparedStatement preparedStatement = null; preparedStatement = connection.prepareStatement(iSql); ResultSet re= preparedStatement.executeQuery(); while (re.next()){ a++; System.out.println(re.getString("goodId")+""+re.getString("goodName")); }if (a!=0){ System.out.println("查询成功"); preparedStatement.close(); connection.close(); return true;} else {preparedStatement.close(); connection.close(); System.out.println("查询失败"); return false; } } public boolean select(String id) throws SQLException { String iSql = "select * from t_good where goodId=?"; PreparedStatement preparedStatement = null; preparedStatement = connection.prepareStatement(iSql); preparedStatement.setString(1,id); ResultSet re= preparedStatement.executeQuery(); while (re.next()){ a++; System.out.println(re.getString("goodId")+""+re.getString("goodName")); }if (a!=0){ System.out.println("查询成功"); preparedStatement.close(); connection.close(); return true;} else {preparedStatement.close(); connection.close(); System.out.println("查询失败"); return false; } } }
service层
IUserService
```clike package org.example.api; import java.sql.SQLException; public interface IUserService { /** * 用户增加接口 * @param * @return boolean */ boolean insertUser(String userName,String userPassword) throws SQLException; /** * 用户登录接口 * @param userName * @return */ boolean loginUser(String userName) throws SQLException; } package org.example.api; import java.sql.SQLException; public interface IOrderService { /** *根据订单id 及 登录的userName来查询订单 * @param orderId * @param userName * @return * @throws SQLException */ boolean selectOrder(String orderId , String userName) throws SQLException; /** * 查询全部订单 * @return * @throws SQLException */ boolean select() throws SQLException; }
IGoodService
package org.example.api; import java.sql.SQLException; public interface IGoodService { /** * 查询全部商品 * @return * @throws SQLException */ boolean selectGood () throws SQLException; /** * 根据商品id查询特定商品 * @param id * @return * @throws SQLException */ boolean select(String id) throws SQLException; }
UserServiceImpl
package org.example.service.impl; import org.example.api.IUserService; import org.example.dao.IUserDao; import org.example.dao.impl.UserDaoImpl; import java.sql.SQLException; import java.sql.SQLRecoverableException; public class UserServiceImpl implements IUserService { IUserDao iUserDao = new UserDaoImpl(); public boolean insertUser(String userName,String userPasswprd) throws SQLException { return iUserDao.loginUser1(userName,userPasswprd); } @Override public boolean loginUser(String userName) throws SQLException { return iUserDao.loginUser(userName); } }
OrderServiceImpl
package org.example.service.impl; import org.example.api.IOrderService; import org.example.dao.IOrderDao; import org.example.dao.impl.OrderDaoImpl; import java.sql.SQLException; public class OrderServiceImpl implements IOrderService { IOrderDao iOrderDao = new OrderDaoImpl(); public boolean selectOrder(String orderId , String userName) throws SQLException { return iOrderDao.selectOrder(orderId,userName) ; } public boolean select() throws SQLException { return iOrderDao.select(); } }
GoodServiceImpl
package org.example.service.impl; import org.example.dao.impl.GoodDaoImpl; import org.example.api.IGoodService; import org.example.api.IGoodService; import org.example.dao.IGoodDao; import java.sql.SQLException; public class GoodServiceImpl implements IGoodService { IGoodDao iGoodDao = new GoodDaoImpl(); @Override public boolean selectGood() throws SQLException { return iGoodDao.selectGood(); } @Override public boolean select(String id) throws SQLException { return iGoodDao.select(id); } }
utils层
JavaDateConnectionUtils
```java package org.example.api.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JavaDateConnectionUtils { /** * 获取数据库连接 * * @return Connection */ public Connection getConn() { String url = "jdbc:mysql://localhost:3306/project?serverTimezone=UTC"; String username = "root"; String password = "Hyk59308"; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); //classLoader,加载对应驱动 conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } }
MyTools
package org.example.api.utils; import java.io.UnsupportedEncodingException; public class MyTools { public static String toChinese(String str) { if(str == null) { str=""; } try { str = new String(str.getBytes("ISO-8859-1"),"gb2312"); }catch(UnsupportedEncodingException e) { str=""; e.printStackTrace(); } return str; } public static int strToInt(String str) { if(str == null || str.equals("")) { str = "0"; } int i = 0; try { i = Integer.parseInt(str); }catch(NumberFormatException e) { i = 0; e.printStackTrace(); } return i; } }
日志配置
<?xml version="1.0" encoding="UTF-8"?> <!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL--> <!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出--> <!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数--> <configuration status="INFO" monitorInterval="30"> <!--先定义所有的appender--> <appenders> <!--这个输出控制台的配置--> <console name="Console" target="SYSTEM_OUT"> <!--输出日志的格式--> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> </console> <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用--> <File name="log" fileName="${sys:user.home}/logs/test.log" append="false"> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> </File> <!-- 这个会打印出所有的info及以下级别的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档--> <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log" filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log"> <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)--> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile> <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log" filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log"> <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> <!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 --> <DefaultRolloverStrategy max="20"/> </RollingFile> <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log" filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log"> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/> <Policies> <TimeBasedTriggeringPolicy/> <SizeBasedTriggeringPolicy size="100 MB"/> </Policies> </RollingFile> </appenders> <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效--> <loggers> <!-- <!–过滤掉spring和mybatis的一些无用的DEBUG信息–>--> <!-- <logger name="org.springframework" level="INFO"></logger>--> <!-- <logger name="org.mybatis" level="INFO"></logger>--> <root level="INFO"> <appender-ref ref="Console"/> <appender-ref ref="RollingFileInfo"/> <appender-ref ref="RollingFileWarn"/> <appender-ref ref="RollingFileError"/> </root> </loggers> </configuration>