ListStu:
1)链接删除信息的Servlet:
"<a href='/JDBC01/ListOne?id="+id+"'>修改 </a></td>"
package case01; 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.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ListStu") public class ListStu extends HttpServlet { private static final long serialVersionUID = 1L; public ListStu() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset = utf-8"); PrintWriter out = response.getWriter(); Connection conn = null; Statement state = null; ResultSet rs = null; // mysql 数据库驱动程序的加载 try { // 加载,固定的语句 Class.forName("com.mysql.jdbc.Driver"); out.print("数据库驱动程序加载成功!"); //数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password = "zzgzzg"; conn = DriverManager.getConnection(url, user, password); out.print("<br>"+"数据库连接成功!"); // 数据库访问并获取结果 state = conn.createStatement(); // sql 语句 String sql = "select id,name,sex,birthday from student"; rs = state.executeQuery(sql); // 画表 out.print("<table border=1>"); // 表头 out.print("<tr>" +"<td>id</td>" +"<td>name</td>" +"<td>sex</td>" +"<td>birthday</td>" +"<td>操作</td>" + "</tr>"); // 循环获取字段数据并输出 while(rs.next()) { int id = rs.getInt(1); // 获取第一个字段,id String name = rs.getString("name"); // 获取名字 String sex = rs.getString("sex"); Date birthday = rs.getDate("birthday"); // 表内容 // 利用<a>标签连接删除、修改的Servlet out.print("<tr>" +"<td>"+id+"</td>" +"<td>"+name+"</td>" +"<td>"+sex+"</td>" +"<td>"+birthday+"</td>" +"<td>" + "<a href='/JDBC01/DropStu?id="+id+"'>删除</a> " + "<a href='/JDBC01/ListOne?id="+id+"'>修改 </a></td>" + "</tr>"); } out.print("</table>"); out.print("<br>"+"数据库访问成功!"); } catch (ClassNotFoundException e) { e.printStackTrace(); out.print("数据库驱动程序加载失败!"); } catch (SQLException e) { e.printStackTrace(); out.print("数据库连接失败!"); }finally { // 回收资源 // 回收 conn if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } // 回收state if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } state = null; } // 回收 rs if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ListOne:
1)根据 id 寻找位置并执行 sql 语句
// 根据 id 寻找位置并执行 sql 语句 int id = Integer.parseInt(request.getParameter("id")); String sql = "select id,sequence,name,sex,birthday from student where id = ?"; PreparedStatement prep = conn.prepareStatement(sql); prep.setInt(1, id); rs = prep.executeQuery();
2)表单 action 属性值为:UpdateStu的绝对位置
"<form action='/JDBC01/UpdateStu' method='post'>"
package case01; 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.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ListOne") public class ListOne extends HttpServlet { private static final long serialVersionUID = 1L; public ListOne() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset = utf-8"); PrintWriter out = response.getWriter(); Connection conn = null; Statement state = null; ResultSet rs = null; // mysql 数据库驱动程序的加载 try { // 加载,固定的语句 Class.forName("com.mysql.jdbc.Driver"); out.print("数据库驱动程序加载成功!"); //数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password = "zzgzzg"; conn = DriverManager.getConnection(url, user, password); out.print("<br>"+"数据库连接成功!"); int id = Integer.parseInt(request.getParameter("id")); // sql 语句 String sql = "select id,sequence,name,sex,birthday from student where id = ?"; // 数据库访问并获取结果 PreparedStatement prep = conn.prepareStatement(sql); prep.setInt(1, id); rs = prep.executeQuery(); // 循环获取字段数据并输出 if(rs.next()) { // int id = rs.getInt(1); // 获取第一个字段,id String sequence = rs.getString("sequence"); String name = rs.getString("name"); // 获取名字 String sex = rs.getString("sex"); Date birthday = rs.getDate("birthday"); out.print("<form action='/JDBC01/UpdateStu' method='post'>"); out.print("<p>" +"<label>学号:</label>" + "<input type='text' name='sequence' value="+sequence+">" + "</p>"); out.print("<p>" +"<label>姓名:</label>" + "<input type='text' name='name' value="+name+">" + "</p>"); out.print("<p>" +"<label>性别:</label>" + "<input type='text' name='sex' value="+sex+">" + "</p>"); out.print("<p>" +"<label>日期:</label>" + "<input type='text' name='birthday' value="+birthday+">" + "</p>"); out.print("<input type='hidden' name='id' value="+id+"></input>"); out.print("<button type='submit'>提交</button>"); out.print("</form>"); } // out.print("<br>"+"数据库访问成功!"); } catch (ClassNotFoundException e) { e.printStackTrace(); out.print("数据库驱动程序加载失败!"); } catch (SQLException e) { e.printStackTrace(); out.print("数据库连接失败!"); }finally { // 回收资源 // 回收 conn if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } // 回收state if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } state = null; } // 回收 rs if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
UpdataStu:
1)信息修改:
// 数据库修改 String sql = "update student set sequence=?,name=?,sex=? where id=?"; PreparedStatement prep = conn.prepareStatement(sql); prep.setString(1, sequence); prep.setString(2, name); prep.setString(3, sex); prep.setInt(4, id); prep.executeUpdate(); //重定向 response.sendRedirect("ListStu");
package case01; 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.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/UpdateStu") public class UpdateStu extends HttpServlet { private static final long serialVersionUID = 1L; public UpdateStu() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset = utf-8"); PrintWriter out = response.getWriter(); Connection conn = null; Statement state = null; ResultSet rs = null; // mysql 数据库驱动程序的加载 try { // 固定的语句 Class.forName("com.mysql.jdbc.Driver"); out.print("数据库驱动程序加载成功!"); //数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/test"; String user = "root"; String password = "zzgzzg"; conn = DriverManager.getConnection(url, user, password); out.print("<br>"+"数据库连接成功!"); // 获得来自前端的参数 int id = Integer.parseInt(request.getParameter("id")); String sequence = request.getParameter("sequence"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String birthday = request.getParameter("birthday"); out.print("<br>"+name+sex+birthday); // 数据库访问 String sql = "update student set sequence=?,name=?,sex=? where id=?"; PreparedStatement prep = conn.prepareStatement(sql); prep.setString(1, sequence); prep.setString(2, name); prep.setString(3, sex); prep.setInt(4, id); prep.executeUpdate(); //重定向 response.sendRedirect("ListStu"); out.print("<br>"+"学生信息添加成功!"); }catch(Exception e) { e.printStackTrace(); out.print("<br>"+"数据库访问失败!"); }finally { // 回收资源 // 回收 conn if(conn!=null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } // 回收state if(state!=null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } state = null; } // 回收 rs if(rs!=null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
整体效果:
修改前:
修改时界面:
修改后: