当前位置: 代码迷 >> Java Web开发 >> 一段代码求助,为什么bool的返回值总是false
  详细解决方案

一段代码求助,为什么bool的返回值总是false

热度:231   发布时间:2010-12-29 15:30:38.0
一段代码求助,为什么bool的返回值总是false
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;
    }
搜索更多相关主题的帖子: localhost  false  mysql  驱动器  null  

----------------解决方案--------------------------------------------------------
你是想检查用户名和密码是否正确吧?如果返回值总是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;
    }
}
----------------解决方案--------------------------------------------------------
程序代码:
while(rs.next()){
                s=rs.getString(2);
                if(s==Password){
                    bool=true;
                }
            }      
你这个 s == Password 不能这样判断啊!!!判断字符串是不是相等应该用 equals()方法。
你把它改成 s.equals(Password) 就OK了。
----------------解决方案--------------------------------------------------------
谢谢你们啊,我已经解决了,还是不熟练啊
----------------解决方案--------------------------------------------------------
  相关解决方案