public int checkDatabase(String name,String password) {
try {
        rs = stmt.executeQuery("select * from uinfo");
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
    try {
        while(rs.next()) {
            if(rs.getString(2).equals(name) && rs.getString(3).equals(password)) {
             return Integer.parseInt(rs.getString(1));
            }
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
    return 0;
}
我这个方法两个参数是用户名和密码,返回用户名的id,如果不存在返回0
数据库中第一列为id,第二列为用户名,第三列为密码
我测试过这段程序if语句应该不对,但是又不知道怎么改,希望高手教我怎么改我这if,多谢了!!

解决方案 »

  1.   

    你数据库里 id name password 都什么类型的?那个是主键?
      

  2.   

    偶了,还有这样判断的直接用SQLConnection con=null;
         PreparedStatement ps=null;
         ResultSet rs=null;
         String sql="select * from uinfo u where u.username=? and u.password=?";
    con =获取数据库连接代码;
    ps=con.prepareStatement(sql);
    ps.setString(1, name);
    ps.setString(2, password);

    rs=ps.executeQuery();

    if(rs!=null && rs.next()){
        return rs.getInt(1);//通过验证
    }else{
        return 0;//用户名密码错误
    }
      

  3.   

    我怎么看不懂你这那句sql语句啊,u是个什么东西啊,本人sql水平有限
      

  4.   

    个人认为,select语句不要用*,直接指出username password
      

  5.   

    id不是主键?name最好弄个唯一键
    3楼的是常用的方法
    也可以先根据name得到记录,然后匹配password,优点在于可以同时判断用户是否存在
      

  6.   

    我是这么创建的表
    create table uinfo
    (userid int,
    uname char(50),
    upassword char(50),
    email char(50),
    sign char(50)
    )
    哪里不合适请大家指出来啊,本人sql比较差劲
      

  7.   

    汗一个,表没什么没问题。
    看下关于主键的资料就行了,也就一两页纸。不知道楼主想完成什么功能,怎么用了这样的思路?
    感觉通过userName取出密码再判断更符合大众思维一些。
      

  8.   

    您在sql语句里u是个什么意思啊?我不太懂
      

  9.   

    select id from uTable u where u.username=? and u.password=?
      

  10.   

    select * from uinfo u where u.username=? and u.password=?
    里边的u是在查询的时候给表uinfo起的别名,对表的引用
    LZ还是恶补一下SQL基础再写代码吧
      

  11.   


            Connection con=null;
            PreparedStatement ps=null;
            ResultSet rs=null;
            String sql="select * from uinfo u where u.username=?";
    con =获取数据库连接代码;
                ps=con.prepareStatement(sql);
                ps.setString(1, name);
                ps.setString(2, password);
                
                rs=ps.executeQuery();
         
             //   if(rs!=null && rs.next()){
             //       return rs.getInt(1);//通过验证
              //  }else{
             //       return 0;//用户名密码错误
              //  }
               while(rs.next()){
                   //判断密码是否正确,决定返回ID还是0
              }我想这么改好一点
      

  12.   

    谢谢您的提醒,我会补sql基础的。
    我复制里您的方法进我的程序,又重新写了数据库,可是返回值总是0,怎么回事啊?还得请您帮帮忙
    表的建立:
    create table uinfo
    (userid int primary key,
    uname varchar(10),
    upassword varchar(10),
    email varchar(15),
    sign varchar(20)
    )
    insert into uinfo values(1,'wx','123','[email protected]','wangxin is me');
    insert into uinfo values(2,'a','123','[email protected]','a is me');
    insert into uinfo values(3,'b','123','[email protected]','b is me');
    insert into uinfo values(4,'c','123','[email protected]','c is me');
    这是那方法:
    public int checkDatabase(String name,String password) throws SQLException {
                    String sql="select * from uinfo u where u.uname=? and u.upassword=?";
    ps=conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            rs=ps.executeQuery();
    if(rs!=null && rs.next()){
                return rs.getInt(1);//通过验证
            }else{
                return 0;//用户名密码错误
            }
    }
      

  13.   

    public int checkDatabase(String name,String password) throws SQLException 
    {
           String sql="select * from uinfo u where u.uname=? and u.upassword=?";
           ps=conn.prepareStatement(sql);
           ps.setString(1, name);
           ps.setString(2, password);
           rs=ps.executeQuery();
           if(rs!=null)
           {
                //return rs.getInt(1)// 获得的是name列
                return rs.getInt(0);//通过验证
           }
           else
           {
                return 0;//用户名密码错误
            }
    }
      

  14.   

    不好意思上面的红色我写错了 , 语言又给搞混了
    ps上面的代码是指楼主只有一条符合条件结果 ;如果有多条符合条件的结果要用while(rs.next()){......}楼主获得的就是一个数组
      

  15.   

    你干嘛把if语句里免得rs.next()删掉啊?还有我照改了,返回值还是0,愁死我了
      

  16.   

    还是返回0说明楼主查找没有成功,也就是没有返回rs
    建议楼主debug一下看看
      

  17.   

    我做了debug,在执行 rs=ps.executeQuery(); 这一行之后rs的值为oracle.jdbc.driver.OracleResultSetImpl@6d2380
    能说明什么啊?返回rs了吗?
      

  18.   


    public int checkDatabase(String name,String password) throws SQLException { 
                    String sql="select * from uinfo u where u.uname=? and u.upassword=?"; 
    ps=conn.prepareStatement(sql); 
            ps.setString(1, name); 
            ps.setString(2, password); 
            rs=ps.executeQuery(); 
    if(rs!=null && rs.next()){ 
                System.out.println("通过验证");//看看这句是否打印
                return rs.getInt("uerid");//通过验证 ,用字段名来取值
            }else{ 
                return 0;//用户名密码错误 
            } 
    }
      

  19.   

    LZ代码里未定义
            Connection con=null;
            PreparedStatement ps=null;
            ResultSet rs=null;
    是否在函数外部定义成类的属性了?
      

  20.   


    能查到这个记录,表示你的数据库里有这条记录同时表明你的username and password 没错不知道这样说能不能理解?
      

  21.   

    //if(rs.getString(2).equals(name) && rs.getString(3).equals(password)) {
    //return Integer.parseInt(rs.getString(1));
    //} 
    我记得index是从0开始的。就是说0-id/1-name/2-password.
      

  22.   

    楼上的和我翻了一样的错误 ,呵呵 这个地方有点特殊了
    getIntpublic int getInt(int columnIndex)
               throws SQLException    Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Java programming language.    Parameters:
            columnIndex - the first column is 1, the second is 2, ... 

        Returns:
            the column value; if the value is SQL NULL, the value returned is 0 
        Throws:
            SQLException - if a database access error occurs引自
    http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html#getInt(int)