今天进行了javaweb分级测试,在三个小时内只完成了基本的教师,学生的增删改查,没有完成其选课的东西。
对于今天的测试,由于其对增删改查的不熟悉,在修改的页面一直出错,修改不进去数据库,所以浪费很长的时间(一个小时左右),在下课后,又进行了javaweb的增删改查的书写
增加了自己的熟悉度。下面是对数据的更改:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.*" %> <% request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); String name = request.getParameter("name"); String gender = request.getParameter("gender"); String class1 = request.getParameter("class"); String major = request.getParameter("major"); // 数据库连接信息 String dbURL = "jdbc:mysql://localhost:3306/demo?useSSL=false"; String dbUsername = "root"; String dbPassword = "123456"; Connection connection = null; PreparedStatement statement = null; ResultSet rs = null; try { // 连接数据库 Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(dbURL, dbUsername, dbPassword); // 检查学生是否存在 String checkSql = "SELECT * FROM market WHERE id = ?"; PreparedStatement checkStatement = connection.prepareStatement(checkSql); checkStatement.setString(1, id); rs = checkStatement.executeQuery(); if (rs.next()) { // 学生存在,更新信息 // 修改学生信息的SQL语句 String updateSql = "UPDATE market SET name = ?, gender = ?,class = ?,major = ? WHERE id = ?"; // 创建PreparedStatement对象 statement = connection.prepareStatement(updateSql); // 设置参数值 statement.setString(1, name); statement.setString(2, gender); statement.setString(3, class1); statement.setString(4, major); statement.setString(5, id); // 执行更新操作 int rowsAffected = statement.executeUpdate(); // 检查更新结果 if (rowsAffected > 0) { out.println("学生信息更新成功!"); } else { out.println("学生信息更新失败!"); } } else { out.println("学生不存在,无法更新信息!"); } } catch (Exception e) { // 处理异常情况 out.println("更新学生信息失败:" + e.getMessage()); } finally { // 关闭ResultSet、PreparedStatement和数据库连接 if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } %>
标签:改查,11.13,增删,println,close,null,out From: https://www.cnblogs.com/dmx-03/p/17830205.html