当用ResultSet接收来自数据库查询的结果集时,即使结果集只有一条数据,也需要用到resultSet.next()函数移动游标获取数据。
错误代码:
conn = DriverManager.getConnection(url, "briup", "briup");
String sql = "select password from e_customer where name=?";
pre = conn.prepareStatement(sql);
pre.setString(1, username);
resultSet = pre.executeQuery();
String passwd = null ;
passwd= resultSet.getString(1);
if(passwd.equals(password)&&password!=null) {
flag = true;
修改后:
conn = DriverManager.getConnection(url, "briup", "briup");
String sql = "select password from e_customer where name=?";
pre = conn.prepareStatement(sql);
pre.setString(1, username);
resultSet = pre.executeQuery();
String passwd = null ;
while (resultSet.next()) {
passwd= resultSet.getString(1);
if(passwd.equals(password)&&password!=null) {
flag = true;
}