当前位置:   article > 正文

击穿JDBC 例子详解 JDBC+Servlet +jsp增删改查案例(三)全_jsp servlet jdbc mysql查询示列

jsp servlet jdbc mysql查询示列

用Servlset + JSP + JDBC 实现增删改查

 

先看看结构

entity是一个学生类,repository是一个与数据库交互的类,servlet与用户交互

关于idea里面连接数据库遇到的时区问题

好了进入正题


先看一下效果

 准备一个学生类(展示少量代码)

  1. public class Student {
  2. private Integer id;
  3. private String username;
  4. public Student(Integer id, String username) {
  5. this.id = id;
  6. this.username = username;
  7. }

 准备一个工具类DBUtil用来快捷处理数据库连接

  1. public class DBUtil {
  2. //静态方法更方便
  3. static Connection connection;
  4. static String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
  5. static String user = "root";
  6. static String password = "123456";
  7. static{
  8. try {
  9. Class.forName("com.mysql.cj.jdbc.Driver");
  10. } catch (ClassNotFoundException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. public static Connection getConnection(){
  15. try {
  16. connection = DriverManager.getConnection(url, user, password);
  17. } catch (SQLException e) {
  18. e.printStackTrace();
  19. }
  20. return connection;
  21. }
  22. public static void close(ResultSet resultSet, Statement statement,Connection connection){
  23. if (resultSet != null) {
  24. try {
  25. resultSet.close();
  26. } catch (SQLException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. if (statement != null) {
  31. try {
  32. statement.close();
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. if (connection != null){
  38. try {
  39. connection.close();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45. }

 这是主页面,用到了forech标签

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <html>
  4. <head>
  5. <title>$Title$</title>
  6. </head>
  7. <body>
  8. <h1>学生管理系统</h1>
  9. <table>
  10. <tr>
  11. <th>编号</th>
  12. <th>姓名</th>
  13. </tr>
  14. <c:forEach items="${list}" var="student">
  15. <tr>
  16. <td>${student.id}</td>
  17. <td>${student.username}</td>
  18. <td><a href="/student?method=deleteBId&id=${student.id}">删除</a></td>
  19. <td><a href="/student?method=findBId&id=${student.id}">修改</a></td>
  20. </tr>
  21. </c:forEach>
  22. </table>
  23. <h3><td><a href="add.jsp">添加</a></td></h3>
  24. </body>
  25. </html>

 这是添加页面add.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. <form action="/student" method="post">
  8. 编号<input type="text" name="id"><br/>
  9. 姓名<input type="text" name="username">
  10. <input type="hidden" name="method" value="add">
  11. <input type="submit" value="提交">
  12. </form>
  13. </body>
  14. </html>

这是修改操作

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>Title</title>
  5. </head>
  6. <body>
  7. <form action="/student" method="post">
  8. 编号<input type="text" name="id" value="${student.id}"><br>
  9. 姓名<input type="text" name="username">
  10. <input type="hidden" name="method" value="update">
  11. <input type="submit" value="修改">
  12. </form>
  13. </body>
  14. </html>

 注意隐藏标签,form表单method都设置是post请求,那么怎么区分呢add和update呢,设置隐藏标签,这样就能区分了

主要要看StudentServlet类,根据这个类去回看jsp,因为他接收jsp传过来的method再去调用数据库操作

  1. @WebServlet("/student")
  2. public class StudentServlet extends HttpServlet {
  3. StudentRepository studentRepository = new StudentRepository();
  4. @Override
  5. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  6. String method = req.getParameter("method");
  7. switch (method){
  8. case "add":
  9. req.setCharacterEncoding("UTF-8");
  10. String idStr = req.getParameter("id");
  11. String username = req.getParameter("username");
  12. Integer id = Integer.parseInt(idStr);
  13. studentRepository.add(id,username);
  14. break;
  15. case "update":
  16. String idStr1 = req.getParameter("id");
  17. String username1 = req.getParameter("username");
  18. Integer id1 = Integer.parseInt(idStr1);
  19. studentRepository.update(id1,username1);
  20. resp.sendRedirect("/student");
  21. }
  22. }
  23. @Override
  24. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  25. String method = req.getParameter("method");
  26. if (method == null) {
  27. method = "findAll";
  28. }
  29. switch (method){
  30. case "findAll":
  31. studentRepository = new StudentRepository();
  32. List<Student> list = studentRepository.findAll();
  33. req.setAttribute("list",list);
  34. req.getRequestDispatcher("index.jsp").forward(req,resp);
  35. break;
  36. case "deleteBId":
  37. String idStr = req.getParameter("id");
  38. Integer id = Integer.parseInt(idStr);
  39. studentRepository.delete(id);
  40. resp.sendRedirect("/student");
  41. break;
  42. case "findBId":
  43. String idStr1 = req.getParameter("id");
  44. Integer id1 = Integer.parseInt(idStr1);
  45. Student student = studentRepository.find(id1);
  46. req.setAttribute("student",student);
  47. req.getRequestDispatcher("update.jsp").forward(req,resp);
  48. break;
  49. }
  50. }
  51. }

 findBId 是在index.jsp里面点击修改,把id参数传了过来,调用find方法把查询结果放在student里,再转到update.jsp,这样编号对应的才是我们要修改的

repository类

  1. public class StudentRepository {
  2. PreparedStatement preparedStatement = null;
  3. ResultSet resultSet = null;
  4. Connection connection = null;
  5. Student student = null;
  6. List<Student> list = new ArrayList<>();
  7. /**
  8. * 查询
  9. * @return
  10. */
  11. public List<Student> findAll() {
  12. try {
  13. Connection connection = DBUtil.getConnection();
  14. String sql = "select * from t_user";
  15. //取得数据库操作对象
  16. preparedStatement = connection.prepareStatement(sql);
  17. resultSet = preparedStatement.executeQuery(sql);
  18. //依次取出
  19. while (resultSet.next()) {
  20. Integer id = resultSet.getInt(1);
  21. String username = resultSet.getString(2);
  22. student = new Student(id,username);
  23. list.add(student);
  24. }
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. } finally {
  28. //关闭流
  29. DBUtil.close(resultSet,preparedStatement,connection);
  30. }
  31. return list;
  32. }
  33. /**
  34. * 添加
  35. * @param id
  36. * @param username
  37. */
  38. public void add(Integer id, String username){
  39. try {
  40. connection = DBUtil.getConnection();
  41. String sql = "insert into t_user(id,username) values (?,?)";
  42. //取得数据库操作对象
  43. preparedStatement = connection.prepareStatement(sql);
  44. preparedStatement.setInt(1,id);
  45. preparedStatement.setString(2,username);
  46. preparedStatement.executeUpdate();
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. } finally {
  50. //关闭流
  51. DBUtil.close(resultSet,preparedStatement,connection);
  52. }
  53. }
  54. /**
  55. * 删除
  56. * @param id
  57. */
  58. public void delete(Integer id){
  59. try{
  60. connection = DBUtil.getConnection();
  61. String sql = "delete from t_user where id = ?";
  62. //取得数据库操作对象
  63. preparedStatement = connection.prepareStatement(sql);
  64. preparedStatement.setInt(1,id);
  65. preparedStatement.executeUpdate();
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }finally {
  69. DBUtil.close(resultSet,preparedStatement,connection);
  70. }
  71. }
  72. /**
  73. * 将需要修改的信息传给student对象
  74. * @param id
  75. */
  76. public Student find(Integer id){
  77. Student student = null;
  78. try{
  79. connection = DBUtil.getConnection();
  80. String sql = "select * from t_user where id = ?";
  81. //取得数据库操作对象
  82. preparedStatement = connection.prepareStatement(sql);
  83. preparedStatement.setInt(1,id);
  84. resultSet = preparedStatement.executeQuery();
  85. //一个结果不用遍历
  86. Integer id1 = resultSet.getInt(1);
  87. String username1 = resultSet.getString(2);
  88. student = new Student(id1,username1);
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }finally {
  92. DBUtil.close(resultSet,preparedStatement,connection);
  93. }
  94. return student;
  95. }
  96. /**
  97. * 修改
  98. */
  99. public void update(Integer id, String username){
  100. try{
  101. connection = DBUtil.getConnection();
  102. String sql = "update t_user set username=? where id=?";
  103. //取得数据库操作对象
  104. preparedStatement = connection.prepareStatement(sql);
  105. preparedStatement.setString(1,username);
  106. preparedStatement.setInt(2,id);
  107. preparedStatement.executeUpdate();
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }finally {
  111. DBUtil.close(resultSet,preparedStatement,connection);
  112. }
  113. }
  114. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/575436
推荐阅读
相关标签
  

闽ICP备14008679号