自定义异常
public class UserNotFoundException extends RuntimeException
{
public UserNotFoundException (String message){ // 构造方法
super(message);
}
}
public static User check (String username, String password) throws UserNotFoundException, PassworkNotCOrrectException
{
User u = null;
Connection conn = DB.getConn();
String sql = "select * from user where username = ' '"+username+" ' ";
Statement stmt = DB.getStatement(conn);
ResultSet rs = DB.getResultSet(stmt , sql);
try{
if( ! rs.next() ){
throw new UserNotFoundException("用户不存在:"+username);
}else{
if(! password.equals(rs.getString(" password ")){
throw new PasswordNotCorrectionException(" 密码输入不正确! ");
}
u = new User();
u.setId ( rs . getInt (" id ") );
...
}catch(SQLExeption e){
e.printStackTrace();
}finally
{
DB.close(rs);
DB.close(stmt);
DB.close(conn);
}
return u;
}