1.将jar包先导入到目录下。
2.导包
<%@ page import="java.sql.*" %>
3.整数据库的一套操作
<% Class.forName("com.mysql.jdbc.Driver"); // 这个是加载数据库驱动 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb","root","123456"); // 这个就是创建管道 Statement statement = connection.createStatement(); // 这个就是创建动力装置 ResultSet resultSet = statement.executeQuery("select * from curd"); %>
先将数据库的信息都查到数据集合中,然后将数据遍历到table中。
4.关于跳转页面
<td> <a href="updateUser1.jsp?id=<%= resultSet.getString("id")%>">修改</a> <a href="deleteUser1.jsp?id=<%= resultSet.getString("id")%>">删除</a> </td>
5.updateUser1.jsp页面
要实现数据回先操作,可以先根据传过来的id进行查询,将整条数据都查找出来,然后将数据回显到页面。
<body> <% try { String id = request.getParameter("id"); System.out.println(id); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb","root","123456"); PreparedStatement preparedStatement = connection.prepareStatement("select * from curd where id = ?"); preparedStatement.setString(1,id); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { %> <%=request.getParameter("id")%> <form action="doUpdateUser1.jsp?id=<%=resultSet.getString("id")%>" method="post"> 请输入新的用户名:<input type="text" name = "username" value= "<%= resultSet.getString("username")%>" /> 请输入新的用户密码:<input type="text" name = "password" value= <%= resultSet.getString("password")%> /> <input type="submit" value="修改"> </form> <% } } catch (Exception e) { out.print(e); } %> </body>
6.doUpdateUser1.jsp页面。
通过request.getParameter(“”)请求获取到表单信息,然后使用sql语句对内容进行修改。
<body> <% try { String id = request.getParameter("id"); String username = request.getParameter("username"); System.out.println("获取到的id是" + id); System.out.println("获取到的username是" + username); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb","root","123456"); PreparedStatement preparedStatement = connection.prepareStatement("update curd set username = ? where id = ?"); preparedStatement.setString(1,username); preparedStatement.setString(2,id); int i = preparedStatement.executeUpdate(); System.out.println("修改成功了嘛" + i); ResultSet resultSet = preparedStatement.executeQuery(); if (i > 0 ) { response.setHeader("refresh","3;url=selectUsers.jsp"); } } catch (Exception e) { } %> </body>
7.整体包结构