<% Student student = new Student(); // student.setNum(request.getParameter("num")); // student.setName(request.getParameter("name")); // StudentDao studentDao = new StudentDao(); studentDao.addStudent(student); %> StudentDao public int addStudent(Student student) throws Exception { 请补充代码使得这个studentDao的添加学生信息的代码完整 }
时间: 2024-03-26 20:36:15 浏览: 55
可以参考以下代码实现:
```
public int addStudent(Student student) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
conn = DBHelper.getConnection();
String sql = "INSERT INTO student(num, name) VALUES(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getNum());
pstmt.setString(2, student.getName());
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.closeConnection(conn, pstmt);
}
return result;
}
```
其中,`DBHelper` 是一个数据库连接工具类,`getConnection()` 方法用于获取数据库连接,`closeConnection()` 方法用于关闭数据库连接。具体实现可以根据自己的需要进行修改。
阅读全文