每日随笔

继续重写选课系统,servlet的创建有些冗杂,可以更加简化

public boolean Insert(student stu) {
boolean f = false;
Connection connection = DBUtil_Student.getConnection();

PreparedStatement preparedStatement = null;
try {
String sql = "insert into student(id,name,sex,classes,major) values ('" + stu.getId() + "','"
+ stu.getName() + "','" + stu.getSex() + "','" + stu.getClasses() + "','" + stu.getMajor() + "')";// 执行语句
preparedStatement = connection.prepareStatement(sql);// 执行语句的转化
preparedStatement.executeUpdate(sql);
f = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// DBUtil.close(resultSet);
DBUtil_Student.close(preparedStatement);
DBUtil_Student.close(connection);
}
return f;
}

@SuppressWarnings("finally")
public student Select(int id) {
String sql = "select * from student where id='" + id + "'";
Connection conn = DBUtil_Student.getConnection();
PreparedStatement st = null;
student stu = null;
ResultSet rs = null;
try {
st = conn.prepareStatement(sql);
st.executeQuery(sql);
rs = st.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String sex = rs.getString("sex");
String classes = rs.getString("classes");
String major = rs.getString("major");
stu = new student(id, name, sex, classes, major);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
DBUtil_Student.close(conn);
DBUtil_Student.close(st);
DBUtil_Student.close(rs);
return stu;
}
}

原文地址:https://www.cnblogs.com/buxiang-Christina/p/14158872.html