- Java code
public static User validate(String username,String password) throws UserNotFoundException, PasswordNotCorrectExeception { Connection conn = null; Statement stmt = null; ResultSet rs = null; User u = null; try { conn = DB.getConn(); stmt = DB.getStmt(conn); String sql = "select password from user where username = '" + username + "'"; rs = DB.executeQuery(stmt, sql); if(!rs.next()) { throw new UserNotFoundException(); } else if (!rs.getString("password").equals(password)) { throw new PasswordNotCorrectExeception(); } else { u = new User(); u.setAddr(rs.getString("addr")); //就是这里开始报错,找不到addr,但是数据库//确实有addr,而且把下面额放上来,都是一样报错,找不到行。 u.setId(rs.getInt("id")); u.setPassword(rs.getString("password")); u.setPhone(rs.getString("phone")); u.setUsername(rs.getString("username")); u.setRdate(rs.getTimestamp("rdate")); } } catch (SQLException e) { e.printStackTrace(); } return u; }
------解决方案--------------------
String sql = "select password from user where username = '" + username + "'";
这一句,查的只有password一个字段,下面取addr,id什么的当然取不到了。
select password,addr,id.....