你是想检查用户名和密码是否正确吧?如果返回值总是FALSE,那肯定是程序执行到boolean bool=false;这句前面还正确,后面的出项问题了,可以在s=rs.getString(2);这句后加上system.out.println("s");将s输出一下,看看能不能得到正确的password。这里面的关键是要和数据库成功连接上,我这有一个自己写的程序,能正确运行,希望对你有所帮助吧!
public class shopManageSystemBean {
private Connection connection=null;
private Statement statement=null;
private ResultSet rs=null;
private String name;
private String password;
public shopManageSystemBean() {
}
public void setUserInfo(String name,String password){
this.name=name;
this.password=password;
}
public String getUserName(){
return this.name;
}
public String getUserPassword(){
return this.password;
}
public boolean isNameExisted(String name){
boolean existed=false;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/shop?user=root&password=dxq";
connection=DriverManager.getConnection(url);
String sql="select * from manager_table where username='"+name+"'";
Statement statement=connection.createStatement();
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
existed=true;
}
}
catch(SQLException ex){
System.out.println("\nERROR:----- SQLException -----\n");
while(ex!=null){
System.out.println("Message: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("ErrorCode: "+ex.getErrorCode());
ex=ex.getNextException();
}
}
catch(Exception ex){
ex.printStackTrace();
}
return existed;
}
public boolean isPassRight(String name,String password){
boolean isRight=false;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/shop?user=root&password=dxq";
connection=DriverManager.getConnection(url);
String sql="select * from manager_table where username='"+name+"'and password='"+password+"'";
Statement statement=connection.createStatement();
ResultSet rs=statement.executeQuery(sql);
if(rs.next()){
isRight=true;
}
}
catch(SQLException ex){
System.out.println("\nERROR:----- SQLException -----\n");
while(ex!=null){
System.out.println("Message: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("ErrorCode: "+ex.getErrorCode());
ex=ex.getNextException();
}
}
catch(Exception ex){
ex.printStackTrace();
}
return isRight;
}
}