11.13
今天进行了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(); } } %>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)