当前位置:   article > 正文

【JSP+JDBC编程】实现简单的数据库增删改查

数据库编程 利用jdbc实现数据库连接和增删改查操作,并在jsp页面中进行显示。

JSP+JDBC技术完成用户管理功能#

【1】主界面模块##

1)index.jsp###

  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2. <%@ page import="java.sql.*"%>
  3. <html>
  4. <head>
  5. <title>My JSP 'index.jsp' starting page</title>
  6. </head>
  7. <body>
  8. <%
  9. String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
  10. Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
  11. Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接
  12. String sql = "select * from users";
  13. PreparedStatement pStmt = conn.prepareStatement(sql);
  14. ResultSet rs = pStmt.executeQuery();
  15. %>
  16. <table border="1" width="500">
  17. <tr>
  18. <th>编号</th>
  19. <th>姓名</th>
  20. <th>密码</th>
  21. <th colspan="2">操作</th>
  22. </tr>
  23. <%
  24. while (rs.next()) {
  25. %>
  26. <tr>
  27. <td>
  28. <%
  29. out.print(rs.getString("id"));
  30. %>
  31. </td>
  32. <td>
  33. <%
  34. out.print(rs.getString("username"));
  35. %>
  36. </td>
  37. <td>
  38. <%
  39. out.print(rs.getString("password"));
  40. %>
  41. </td>
  42. <td><a href="delete.jsp?id=<%=rs.getString("id")%>" onclick="return confirm('确定删除该条记录?')">删除</a></td>
  43. <td><a href="edit.jsp?id=<%=rs.getString("id")%>">编辑</a></td>
  44. </tr>
  45. <%
  46. }
  47. %>
  48. </table>
  49. </br>
  50. <a href="register.jsp">添加新记录</a>
  51. <%
  52. //关闭
  53. rs.close();
  54. pStmt.close();
  55. conn.close();
  56. %>
  57. </body>
  58. </html>

新建模块##

1)register.jsp###

  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2. <html>
  3. <head>
  4. <title>My JSP 'register.jsp' starting page</title>
  5. </head>
  6. <body>
  7. <form action="add.jsp">
  8. 姓名:<input type="text" name="username"/><br>
  9. 密码:<input type="text" name="password"/><br><br>
  10. <input type="submit" value="添加" />
  11. </form>
  12. </body>
  13. </html>

2)add.jsp###

  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2. <%@ page import="java.sql.*"%>
  3. <html>
  4. <head>
  5. <title>My JSP 'add.jsp' starting page</title>
  6. </head>
  7. <body>
  8. <%
  9. String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
  10. Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
  11. Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接
  12. String sql = "insert into users values(null,?,?)";
  13. PreparedStatement pStmt = conn.prepareStatement(sql);
  14. String username = request.getParameter("username");
  15. String password = request.getParameter("password");
  16. pStmt.setString(1, username);
  17. pStmt.setString(2, password);
  18. int cnt = pStmt.executeUpdate();
  19. if (cnt > 0) {
  20. response.sendRedirect("index.jsp");
  21. } else {
  22. out.print("用户已存在,<a href='register.jsp'>重新注册</a>");
  23. }
  24. //关闭
  25. pStmt.close();
  26. conn.close();
  27. %>
  28. </body>
  29. </html>

编辑模块##

1)edit.jsp###

  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2. <%@ page import="java.sql.*"%>
  3. <html>
  4. <head>
  5. <title>My JSP 'edit.jsp' starting page</title>
  6. </head>
  7. <body>
  8. <%
  9. String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
  10. Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
  11. Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接
  12. String id = request.getParameter("id");
  13. String username = "";
  14. String password = "";
  15. String sql = "select * from users where id=?";
  16. PreparedStatement pStmt = conn.prepareStatement(sql);
  17. pStmt.setString(1, id);
  18. ResultSet rs = pStmt.executeQuery();
  19. while (rs.next()) {
  20. username = rs.getString("username");
  21. password = rs.getString("password");
  22. }
  23. %>
  24. <form action="modify.jsp">
  25. 编号:<input type="text" name="id" readonly="readonly" value="<%=id %>" /><br>
  26. 姓名:<input type="text" name="username" value="<%=username %>" /><br>
  27. 密码:<input type="text" name="password" value="<%=password %>"/><br><br>
  28. <input type="submit" value="修改" />
  29. </form>
  30. <%
  31. //关闭
  32. pStmt.close();
  33. conn.close();
  34. %>
  35. </body>
  36. </html>

2)modify.jsp###

  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2. <%@ page import="java.sql.*"%>
  3. <html>
  4. <head>
  5. <title>My JSP 'modify.jsp' starting page</title>
  6. </head>
  7. <body>
  8. <%
  9. String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
  10. Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
  11. Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接
  12. String sql = "update users set username=?,password=? where id=?";
  13. PreparedStatement pStmt = conn.prepareStatement(sql);
  14. String id = request.getParameter("id");
  15. String username = request.getParameter("username");
  16. String password = request.getParameter("password");
  17. pStmt.setString(1, username);
  18. pStmt.setString(2, password);
  19. pStmt.setString(3, id);
  20. pStmt.executeUpdate();
  21. //关闭
  22. pStmt.close();
  23. conn.close();
  24. response.sendRedirect("index.jsp");
  25. %>
  26. </body>
  27. </html>

删除模块##

1)delete.jsp###

  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2. <%@ page import="java.sql.*"%>
  3. <html>
  4. <head>
  5. <title>My JSP 'delete.jsp' starting page</title>
  6. </head>
  7. <body>
  8. <%
  9. String url = "jdbc:mysql://localhost:3306/testdb"; //数据库连接串
  10. Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
  11. Connection conn = DriverManager.getConnection(url, "root", "root"); //建立连接
  12. String sql = "delete from users where id=?";
  13. PreparedStatement pStmt = conn.prepareStatement(sql);
  14. String id = request.getParameter("id");
  15. pStmt.setString(1, id);
  16. pStmt.executeUpdate();
  17. //关闭
  18. pStmt.close();
  19. conn.close();
  20. response.sendRedirect("index.jsp");
  21. %>
  22. </body>
  23. </html>

转载于:https://my.oschina.net/sugary/blog/673822

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

闽ICP备14008679号