public static boolean dbLogin(String username,String password){
boolean res=false;
DBConnection dbc=new DBConnection();
Connection con=dbc.dbcon();
String sql="select user_Name from u where user_Pwd="+password;
try {
ResultSet rs=con.createStatement().executeQuery(sql);
if(rs.next()){
String user_Name=rs.getString(1);
System.out.print(user_Name);
if(user_Name.equals(username)){
res=true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
boolean res=false;
DBConnection dbc=new DBConnection();
Connection con=dbc.dbcon();
String sql="select user_Name from u where user_Pwd="+password;
try {
ResultSet rs=con.createStatement().executeQuery(sql);
if(rs.next()){
String user_Name=rs.getString(1);
System.out.print(user_Name);
if(user_Name.equals(username)){
res=true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
正确的为:String user_Name=rs.getString(0);
boolean res=false;
DBConnection dbc=new DBConnection();
Connection con=dbc.dbcon();
String sql="select user_Name from u where user_Pwd="+password;
try {
ResultSet rs=con.createStatement().executeQuery(sql);
if(rs.next()){
String user_Name=rs.getString(1);
System.out.print(user_Name);//可以打印
if(user_Name.equals(username)){
//执行不了
res=true;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
if(user_Name.equals(username)){
//执行不了
res=true;
}
是不是username为null啊?
把username打印出来看一下好了
//执行不了
res=true;
} 还不行
就是这个if语句执行不了
if(rs.next()){
String user_Name=rs.getString(1);
System.out.print(user_Name);//可以打印
if(user_Name.equals(username)){
//执行不了
res=true;
} 改成:
ResultSet rs=con.createStatement().executeQuery(sql);
while(rs!=null&&rs.next()){
String user_Name=rs.getString(1);
System.out.print(user_Name);
if(user_Name!=null&&user_Name.equals(username)){
res=true;
}
user_Name和username全都打印出来看看,好不
用length()来测试下两个值的长度是不是一样!
要么username.trim().equals试试,要么类型改成varchar试试
name password
里 123456
外 123456你怎么办?
if rs.getInt(1)>=0
res = true;
这样才能判断是否有这个用户存在
你的password是String类型的,那么你的sql语句就有问题了
把String sql="select user_Name from u where user_Pwd="+password;
改成2楼的试试String sql="select user_Name from u where user_Pwd='"+password+"'";