<%@ 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();
}
}
%>