每周总结
今天对对数据库的添加做了练习,对其中语句的意义进行了查询
public void add(student stu) {
Connection connection = DUBI_Student/* 连接数据库文件名 */.getConnection();
System.out.println(connection);
PreparedStatement preparedStatement = null;
try {
String sql = "insert into student(id,name,sex,classes,major) values (?,?,?,?,?)";// 执行语句
preparedStatement = connection.prepareStatement(sql);// 执行语句的转化
preparedStatement.setInt(1, stu.getId());
preparedStatement.setString(2, stu.getName());
preparedStatement.setString(3, stu.getSex());
preparedStatement.setString(4, stu.getClasses());
preparedStatement.setString(5, stu.getMajor());
preparedStatement.executeUpdate();
System.out.println("添加成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// DBUtil.close(resultSet);
DUBI_Student.close(preparedStatement);
DUBI_Student.close(connection);
}
}

浙公网安备 33010602011771号