//验证用户是否存在
public int userLogin(Users user) throws SQLException{
String sql="select * from users where userName=? and password=?";
getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
rs=pstmt.executeQuery();
return rs.getRow();
}
为什么返回的结果总是0啊?
public int userLogin(Users user) throws SQLException{
String sql="select * from users where userName=? and password=?";
getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
rs=pstmt.executeQuery();
return rs.getRow();
}
为什么返回的结果总是0啊?
解决方案 »
- SSH使用oracle配置问题
- 如何设计一个树状结构,求思路
- 用XStream删除xml根节点,添加xml根节点怎么做
- jsf developerworks 复合组件 managedbean scope
- cookie的存储
- 客户端访问tomcat,抢占80端口以后不松开。是什么问题啊?
- java outlook-calendar的问题
- 求助:用hibernate向数据库中插入数据后,数据库中显示乱码
- 打成jar包就不能被sping的context:component-scan找到了吗?
- 本人是西工大计算机应用专业的研究生 马上就要开题了
- 我为什么不能把从数据库读出来的字符串赋值给String的变量String str=rs.getString("name");
- 谁有 Ajax 的 顶 功能啊
用select count(*) as c,最后用ResultSet去把c的值拿出来比较保险。
总是0可能是某些jdbc驱动并未实现这个抽象方法,或者某些数据库的问题。(某版本的access有此问题)
另外也可以return rs==null?0:1;//0无用户,1有用户
select count(userName) from users where userName=? and password=?然后使用:
if(rs.next()) {
return rs.getInt(1);//返回数量
}
return 0;//如果前面没执行完毕记得返回一个0。最后记得把一些该关闭的东西关闭掉。
这句才是问题关键啊,rs指向的不是结果行,要先调用rs.next()才行
public int userLogin(Users user) throws SQLException{
String sql="select * from users where userName=? and password=?";
getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
rs=pstmt.executeQuery();
int number=0;
while(rs.next()){
number=1;
}
return number;
如果number=1说明登陆正确,你要是想得到对象就在while循环中接受
}
在他下边用while()循环一下数据库
然后在判断一下用户名存不存在。
如果存在返回true,不存在返回false。