请修改下面的两段代码使之能运行, 代码 1<body> <%request.setCharacterEncoding("utf-8");//解决提交乱码问题 %> <% String stuno = request.getParameter("stuno"); String stuname = request.getParameter("stuname"); if (stuno != null) { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql: //127.0.0.1:3306/sys", "root", "1234"); PreparedStatement ps = null; ResultSet rs = null; String sql = "SELECT num FROM tb_student WHERE NUM=?"; ps = conn.prepareStatement(sql); ps.setString(1, stuno); rs = ps.executeQuery(); if (rs.next()) { out.println("对不起,该学生学号已经存在!"); } else { sql = "INSERT INTO tb_student(NUM,NAME) VALUES(?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, stuno); ps.executeUpdate(); out.println("添加成功!"); } } %> 添加学生信息 <br> <form method="post"> 输入学号:<input type="text" name="stuno"><BR> 输入姓名:<input type="text" name="stuname"><BR> <input type="submit" value="添加"> </form> </body> </html>,代码2 // 添加学生信息到数据中 public int addStudent(Student student) throws Exception { initConnection(); // 创建一个sql语句 String sql = "insert into tb_student(num,name) values (?,?)"; // 创建一个PreparedStatement对象 PreparedStatement ps = conn.prepareStatement(sql); // 填充参数 ps.setString(1, student.getNum()); ps.setString(2, student.getName()); // 执行sql语句 int retv = ps.executeUpdate(); ps.close(); // 关闭链接 closeConnection(); return retv; }
时间: 2024-03-26 16:34:51 浏览: 87
代码1修改后的代码如下:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");//解决提交乱码问题
String stuno = request.getParameter("stuno");
String stuname = request.getParameter("stuname");
if (stuno != null) {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sys?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "1234");
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT num FROM tb_student WHERE NUM=?";
ps = conn.prepareStatement(sql);
ps.setString(1, stuno);
rs = ps.executeQuery();
if (rs.next()) {
out.println("对不起,该学生学号已经存在!");
} else {
sql = "INSERT INTO tb_student(NUM,NAME) VALUES(?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, stuno);
ps.setString(2, stuname);
ps.executeUpdate();
out.println("添加成功!");
}
rs.close();
ps.close();
conn.close();
}
%>
添加学生信息 <br>
<form method="post">
输入学号:<input type="text" name="stuno"><BR>
输入姓名:<input type="text" name="stuname"><BR>
<input type="submit" value="添加">
</form>
</body>
</html>
```
代码2修改后的代码如下:
```java
// 添加学生信息到数据中
public int addStudent(Student student) throws Exception {
initConnection();
// 创建一个sql语句
String sql = "insert into tb_student(num,name) values (?,?)";
// 创建一个PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 填充参数
ps.setString(1, student.getNum());
ps.setString(2, student.getName());
// 执行sql语句
int retv = ps.executeUpdate();
ps.close();
// 关闭链接
closeConnection();
return retv;
}
```
阅读全文