| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 924 人关注过本帖
标题:一段代码求助,为什么bool的返回值总是false
只看楼主 加入收藏
wolf_howl
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-10-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
一段代码求助,为什么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 
2010-12-29 15:30
曲水流觞___
Rank: 1
来 自:郑州
等 级:新手上路
帖 子:15
专家分:8
注 册:2010-11-25
收藏
得分:8 
你是想检查用户名和密码是否正确吧?如果返回值总是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;
    }
}
2010-12-31 13:31
wzh545394622
Rank: 6Rank: 6
等 级:侠之大者
帖 子:60
专家分:410
注 册:2009-10-7
收藏
得分:12 
程序代码:
 while(rs.next()){
                s=rs.getString(2);
                if(s==Password){
                    bool=true;
                }
            }       
你这个 s == Password 不能这样判断啊!!!判断字符串是不是相等应该用 equals()方法。
你把它改成 s.equals(Password) 就OK了。
2010-12-31 20:51
wolf_howl
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-10-3
收藏
得分:0 
谢谢你们啊,我已经解决了,还是不熟练啊
2011-01-02 01:30
快速回复:一段代码求助,为什么bool的返回值总是false
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048834 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved