public int checkDatabase(String name,String password) {
try {
rs = stmt.executeQuery("select * from uinfo");
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
while(rs.next()) {
if(rs.getString(2).equals(name) && rs.getString(3).equals(password)) {
return Integer.parseInt(rs.getString(1));
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return 0;
}
我这个方法两个参数是用户名和密码,返回用户名的id,如果不存在返回0
数据库中第一列为id,第二列为用户名,第三列为密码
我测试过这段程序if语句应该不对,但是又不知道怎么改,希望高手教我怎么改我这if,多谢了!!
try {
rs = stmt.executeQuery("select * from uinfo");
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
while(rs.next()) {
if(rs.getString(2).equals(name) && rs.getString(3).equals(password)) {
return Integer.parseInt(rs.getString(1));
}
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return 0;
}
我这个方法两个参数是用户名和密码,返回用户名的id,如果不存在返回0
数据库中第一列为id,第二列为用户名,第三列为密码
我测试过这段程序if语句应该不对,但是又不知道怎么改,希望高手教我怎么改我这if,多谢了!!
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from uinfo u where u.username=? and u.password=?";
con =获取数据库连接代码;
ps=con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs!=null && rs.next()){
return rs.getInt(1);//通过验证
}else{
return 0;//用户名密码错误
}
3楼的是常用的方法
也可以先根据name得到记录,然后匹配password,优点在于可以同时判断用户是否存在
create table uinfo
(userid int,
uname char(50),
upassword char(50),
email char(50),
sign char(50)
)
哪里不合适请大家指出来啊,本人sql比较差劲
看下关于主键的资料就行了,也就一两页纸。不知道楼主想完成什么功能,怎么用了这样的思路?
感觉通过userName取出密码再判断更符合大众思维一些。
里边的u是在查询的时候给表uinfo起的别名,对表的引用
LZ还是恶补一下SQL基础再写代码吧
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from uinfo u where u.username=?";
con =获取数据库连接代码;
ps=con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
rs=ps.executeQuery();
// if(rs!=null && rs.next()){
// return rs.getInt(1);//通过验证
// }else{
// return 0;//用户名密码错误
// }
while(rs.next()){
//判断密码是否正确,决定返回ID还是0
}我想这么改好一点
我复制里您的方法进我的程序,又重新写了数据库,可是返回值总是0,怎么回事啊?还得请您帮帮忙
表的建立:
create table uinfo
(userid int primary key,
uname varchar(10),
upassword varchar(10),
email varchar(15),
sign varchar(20)
)
insert into uinfo values(1,'wx','123','[email protected]','wangxin is me');
insert into uinfo values(2,'a','123','[email protected]','a is me');
insert into uinfo values(3,'b','123','[email protected]','b is me');
insert into uinfo values(4,'c','123','[email protected]','c is me');
这是那方法:
public int checkDatabase(String name,String password) throws SQLException {
String sql="select * from uinfo u where u.uname=? and u.upassword=?";
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs!=null && rs.next()){
return rs.getInt(1);//通过验证
}else{
return 0;//用户名密码错误
}
}
{
String sql="select * from uinfo u where u.uname=? and u.upassword=?";
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs!=null)
{
//return rs.getInt(1)// 获得的是name列
return rs.getInt(0);//通过验证
}
else
{
return 0;//用户名密码错误
}
}
ps上面的代码是指楼主只有一条符合条件结果 ;如果有多条符合条件的结果要用while(rs.next()){......}楼主获得的就是一个数组
建议楼主debug一下看看
能说明什么啊?返回rs了吗?
public int checkDatabase(String name,String password) throws SQLException {
String sql="select * from uinfo u where u.uname=? and u.upassword=?";
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs!=null && rs.next()){
System.out.println("通过验证");//看看这句是否打印
return rs.getInt("uerid");//通过验证 ,用字段名来取值
}else{
return 0;//用户名密码错误
}
}
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
是否在函数外部定义成类的属性了?
能查到这个记录,表示你的数据库里有这条记录同时表明你的username and password 没错不知道这样说能不能理解?
//return Integer.parseInt(rs.getString(1));
//}
我记得index是从0开始的。就是说0-id/1-name/2-password.
getIntpublic int getInt(int columnIndex)
throws SQLException Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language. Parameters:
columnIndex - the first column is 1, the second is 2, ...
Returns:
the column value; if the value is SQL NULL, the value returned is 0
Throws:
SQLException - if a database access error occurs引自
http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html#getInt(int)