每日随笔

今天写了第二个自测题,其中遇到了错误拦截的问题,对结构流程还是不够熟练

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

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

public boolean delete(int id) {
String sql = "delete from student where id='" + id + "'";
boolean f = false;
Connection conn = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate(sql);
f = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
DBUtil.close(preparedStatement);
}
return f;
}

public boolean update(int id, int score, String ke) {
String sql = "update student set score='" + score + "'where id='" + id + "'and ke='" + ke + "'";
Connection conn = DBUtil.getConnection();
boolean f = false;
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate(sql);
f = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
DBUtil.close(preparedStatement);
}
return f;
}

public student search(String name) throws Exception {
int id = Integer.parseInt(name);
String sql = "select * from student where name like '%" + name + "%' or id like '%" + id + "%' ";
Connection conn = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
PreparedStatement pt = conn.prepareStatement(sql);
ResultSet rs = null;
student stu = null;
try {
pt = conn.prepareStatement(sql);
rs = pt.executeQuery();
while (rs.next()) {
int id1 = rs.getInt("id");
String name1 = rs.getString("name");
String classes = rs.getString("classes");
String ke = rs.getString("ke");
int score = Integer.parseInt(rs.getString("score"));
stu = new student(id1, name1, classes, ke, score);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
DBUtil.close(preparedStatement);
DBUtil.close(rs);
}
return stu;
}

public List<student> searchByid(int id) throws Exception {
String sql = "select * from student where id like '%" + id + "%' ";
Connection conn = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
PreparedStatement pt = conn.prepareStatement(sql);
ResultSet rs = null;
student stu = null;
List<student> list = new ArrayList<>();
try {
pt = conn.prepareStatement(sql);
rs = pt.executeQuery();
while (rs.next()) {
int id1 = rs.getInt("id");
String name1 = rs.getString("name");
String classes = rs.getString("classes");
String ke = rs.getString("ke");
int score = Integer.parseInt(rs.getString("score"));
stu = new student(id1, name1, classes, ke, score);
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
DBUtil.close(preparedStatement);
DBUtil.close(rs);
}
return list;
}

public List<student> searchByidandname(String id) throws Exception {
int id1 = Integer.parseInt(id);
String sql = "select * from student where id like '%" + id1 + "%'or name like '%" + id + "%'";
Connection conn = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
PreparedStatement pt = conn.prepareStatement(sql);
ResultSet rs = null;
student stu = null;
List<student> list = new ArrayList<>();
try {
pt = conn.prepareStatement(sql);
rs = pt.executeQuery();
while (rs.next()) {
int id2 = rs.getInt("id");
String name1 = rs.getString("name");
String classes = rs.getString("classes");
String ke = rs.getString("ke");
int score = Integer.parseInt(rs.getString("score"));
stu = new student(id2, name1, classes, ke, score);
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn);
DBUtil.close(preparedStatement);
DBUtil.close(rs);
}
return list;
}

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