![]() |
#2
曲水流觞___2010-12-31 13:31
你是想检查用户名和密码是否正确吧?如果返回值总是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; } } |
public boolean checklogin(String Username,String Password) {
String url = "jdbc:mysql://localhost/jspwork";
String userName = "root";
String password = "root";
String sql = null;
Connection conn = null;
Statement stmt = null;
String s;
boolean bool=false;
try {
//第一步:加载驱动器
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.err.print("ClassNotFoundException");
}
try {
//第二步:调用DriverManager.getConnection静态方法得到数据库连接
conn = DriverManager.getConnection(url, userName, password);
//创建Statement语句
stmt = conn.createStatement();
sql =" select * from userfo where username="+"\'"+Username+"\'";
//使用Statement语句对象执行SQL语句
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
s=rs.getString(2);
if(s==Password){
bool=true;
}
}
} catch(SQLException e) {
// System.err.println("Query SQLException");
} finally {
//关闭语句和数据库连接
try {
stmt.close();
conn.close();
} catch(SQLException e) {
System.err.println("Close SQLException");
}
}
return bool;
}