当前位置: 代码迷 >> J2EE >> 请问,这段代码如何老是报错
  详细解决方案

请问,这段代码如何老是报错

热度:60   发布时间:2016-04-22 01:26:42.0
请教,这段代码怎么老是报错
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.....
  相关解决方案