处理异常返回值,代码调优

This commit is contained in:
myh 2023-10-07 19:54:26 +08:00
parent 38b4554c98
commit b33ec7e404

View File

@ -19,6 +19,7 @@ public class UsersDaoImpl implements UsersDao {
private PreparedStatement preparedStatement = null; private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null; private ResultSet resultSet = null;
DataBaseUtil dataBaseUtil = new DataBaseUtil(); DataBaseUtil dataBaseUtil = new DataBaseUtil();
//新建用户 //新建用户
@Override @Override
public int insert(@NotNull Users users) { public int insert(@NotNull Users users) {
@ -46,7 +47,7 @@ public class UsersDaoImpl implements UsersDao {
@Override @Override
public int delete(@NotNull Users user) { public int delete(@NotNull Users user) {
String delete_sql = "delete from Users where id=?"; String delete_sql = "delete from Users where id=?";
CommonDao<Users> dao = new CommonDao<>(); CommonDao dao = new CommonDao();
return dao.delete(connection, preparedStatement, resultSet, return dao.delete(connection, preparedStatement, resultSet,
delete_sql, user.getId()); delete_sql, user.getId());
} }
@ -121,8 +122,13 @@ public class UsersDaoImpl implements UsersDao {
dataBaseUtil.close(connection, preparedStatement, resultSet); dataBaseUtil.close(connection, preparedStatement, resultSet);
} }
if (user == null) {
throw new RuntimeException("用户不存在");
} else {
return user; return user;
} }
}
private void SetAttribute(@NotNull Users user, @NotNull ResultSet rs) throws SQLException { private void SetAttribute(@NotNull Users user, @NotNull ResultSet rs) throws SQLException {
user.setId(rs.getLong("id")); user.setId(rs.getLong("id"));
user.setName(rs.getString("name")); user.setName(rs.getString("name"));