写一个登陆程序,要求输入ID,密码.用文本域的getText()方法获得输入的文本,如何从数据库中选出ID,MIMA的项,并与用户输入的ID,MIMA作比较?

解决方案 »

  1.   

    select id,mima from tb where id=.getText() and mima=.getPassword();
    如果有记录则表明用户名密码输入正确,否则不正确
      

  2.   

    下面的这个你作为参考:String name, String pwd 这两个为你输入ID,密码.用文本域的getText()方法获得输入的文本参数
    public int userIsOk(String name, String pwd){

    String str = "select * from companylist where account ='"+name+"' and password ='"+pwd+"'";//这里改为你的数据库查询

    try {
    sqlStmt = sqlConn.createStatement();
    ResultSet rst = sqlStmt.executeQuery(str);
    if(rst.next()){
    return 1;
    }else{
    return 0;}
    } catch (SQLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    return 0;
    }


    }
      

  3.   

    try { 
    sqlStmt = sqlConn.createStatement(); 
    ResultSet rst = sqlStmt.executeQuery(str); 
    if(rst.next()){ 
    return 1; 
    }else{ 
    return 0;} 
    } catch (SQLException e) { 
    // TODO 自动生成 catch 块 
    e.printStackTrace(); 
    return 0; 

      

  4.   

     我的数据库是javashuju2,
    下面是我编写的,当我注册成功以后进行登陆.ID 密码都和注册时的一样。但是就是不能登陆。老是执行JOptionPane.showMessageDialog(null,"您的ID或者密码错误请重新登陆" );这一句。您能给我检查一下吗?
    Statement s;ResultSet r;
    JTextField jt1=new JTextField(15);
    JTextField jp=new JTextField(15);ID1=jt1.getText();
    mima1=jp.getText();
    try{  Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
                 Connection c=DriverManager.getConnection("jdbc:odbc:javashuju2", "","");
                 s=c.createStatement();
           
             }catch(Exception g){System.out.print(g.toString());}
          try{ 
           r=s.executeQuery("SELECT * FROM javashuju2 WHERE ID='"+ ID1+"' and mima='"+mima1+"'  " );
          
          }catch(Exception k){System.out.print(k.toString());}    
         try{   
              if(r.next())
                 {              new liaotian();//主聊天程序
                        
                  }
                 else
                     JOptionPane.showMessageDialog(null,"您的ID或者密码错误请重新登陆" );
          
         }  catch(Exception m){}
      

  5.   

    select id,mima from tb where id=.getText() and mima=.getPassword(); 
      

  6.   

      //输入的值和数据库中的值做比较,返回真假
        public boolean comPare(String id,String pwd)
        {
            try {
                //打开数据库的方法
                this.OpenSql();
                //执行查询语句,根据你传过来的id和密码作为查询条件。
                ResultSet rs = st.executeQuery("select * from Datas where Id ='"+id+"' and PassWord ='"+pwd+"'");
                //如果结果集的值不为空,那么就返回真,也就是有改用户,或者返回假,没有改用户
                if(rs != null){
                    return true;
                }else
                {
                    return false;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            }finally{
                //关闭数据库的方法
                this.CloseSql();
            } 
        }
      

  7.   

    public boolean landing(String id,String mima)
       try{
             boolean check = false;         
             Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); 
             Connection con = DriverManager.getConnection("jdbc:odbc:test", "sa",""); 
             String sql = "select count(*) from DatabaseName where id=? and mima= ?";
             pstat.setString(1,id);
             pstat.setString(2,mima);
             PreparedStatement pstat = con.prepareStatement(sql);
             ResultSet res = pstat.executeQuery();      
             while(res.next())
             {
                check = true; 
             }
           con.close();
          }catch(Exception ex)
          {
              System.out.println(ex);
          }
          return check;
    }