怎么重写user.equals(a),
用户登陆这一块就是搞不定呀
就卡到密码与iD的比较相等上了,
有人能帮着写一下吗???user.equals(a),可能要重写.equals()package storemanager_class;
import storemanager.*;
import java.sql.*;
public class is_password {private String s;
private String t;
  public is_password() { }  //登陆界面核ID和密码:
  public int init(String a,String b) {     //a,b 是传入的  id 和  password参数
        is_password v1 =new is_password();
     try {       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动
       System.out.println("driver is oK");
       Connection con = DriverManager.getConnection(
           "jdbc:microsoft:sqlserver://LENOVO-320EB584:1433;DatabaseName=stock",
           "sa", "123");
    
       Statement sta = con.createStatement();
       ResultSet rs = sta.executeQuery("select  *from holder_login"); //executeQuery
       while (rs.next()) {
         String user=new String();
         String pwd=new String();
         user= rs.getString("ID").toString();
         pwd=rs.getString("密码").toString();
     if(user.equals(a)) && pwd.equals(b))
         { 
           return 1;
         }
       }
     }
       catch (Exception e) {
         e.printStackTrace(); //printStackTrace();
             }
        
       return 0;
     }  public static void main(String[] args) {
    is_password is_password = new is_password();  }
}

解决方案 »

  1.   

    控制台打印出来看下最简单了..不要没取到或者本来就不等就在比较了...然后不要忘了trim()去掉首尾的空格String的equals()重写它干嘛 @_@
      

  2.   

    不是,我是想匹配,用户输入的用户名和密码与数据库里的用户名和密码是否相同.
    也就是字符串比较,不过我用了a.equals(b),不行呀,现在系统里只要跟判断字符串是否相等有关的地方都出不来呀
    谁能指点一下?????字符串比较,我的可能字符串是传进来的值.
      

  3.   

    我把你的代码做了下修改,,你注意我修改的地方
    package storemanager_class; 
    import storemanager.*; 
    import java.sql.*; 
    public class is_password { private String s; 
    private String t; 
      public is_password() { }   //登陆界面核ID和密码: 
      public int init(String a,String b) {     //a,b 是传入的  id 和  password参数 
            is_password v1 =new is_password(); 
         try {        Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动 
           System.out.println("driver is oK"); 
           Connection con = DriverManager.getConnection( 
               "jdbc:microsoft:sqlserver://LENOVO-320EB584:1433;DatabaseName=stock", 
               "sa", "123"); 
         
           Statement sta = con.createStatement(); 
           ResultSet rs = sta.executeQuery("select  *from holder_login"); //executeQuery 
           while (rs.next()) { 
             String user=new String(); 
             String pwd=new String(); 
            //你原来是这样的
             //user= rs.getString("ID").toString(); 
             //pwd=rs.getString("密码").toString(); 
             /*-----我做的改动------*/
             user= rs.getString("ID").toString().trim(); 
             pwd=rs.getString("密码").toString().trim();
            //注意这里。 在a和b后面都去掉他的空格
         if(user.equals(a.trim())) && pwd.equals(b.trim())) 
             {  
               return 1; 
             } 
           } 
         } 
           catch (Exception e) { 
             e.printStackTrace(); //printStackTrace(); 
                 } 
             
           return 0; 
         }   public static void main(String[] args) { 
        is_password is_password = new is_password();   } 
    }
    这个问题经常出现的,,是在插入数据的时候,,你本来要插入sky_ccy可你不小心,,
    让他成为了sky_ccy 后面多了个空格,,那样你在比较的时候,拿“sky_ccy ”和“sky_ccy”比较
    时会出问题的,,还有就是
    你的这个程序设计本来就不合理的,,有机会好好看看DAO设计模式,,
    把他与数据库交互的部分独立出来,,
      

  4.   

    还有在插入数据的时候一定要做个限制,,
    例如你要插入password,,在插入前,,先做个过滤,,
    password=password.trim();
    我举这个例子只是来说下情况,,
      

  5.   

    3q,我在搞毕业设计,代码写的超乱,
      自己就没学过java,第一次写就写个管理系统,超郁闷了!
      

  6.   


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    //整理了一下,主要是没必要的多了,加了个trim(),加了个close()方法
    public class IdPass {
      //登陆界面核ID和密码:  
      public int init(String a,String b) {     //a,b 是传入的  id 和  password参数  
      Connection con = null;
      Statement sta = null;
      ResultSet rs = null;
         try {          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动  
           System.out.println("driver is oK");  
           con = DriverManager.getConnection(  
               "jdbc:microsoft:sqlserver://LENOVO-320EB584:1433;DatabaseName=stock",  
               "sa", "123");  
          
           sta = con.createStatement();  
           rs = sta.executeQuery("select  *from holder_login"); //executeQuery  
           while (rs.next()) {   
            
             String user= rs.getString("ID").toString().trim();  
             String pwd=rs.getString("密码").toString().trim(); 
            
           if(user.equals(a.trim()) && pwd.equals(b.trim())) return 1;           
            
           }  
         }  
         catch (Exception e) {  
        
         try {
         rs.close();
         sta.close();
    con.close();
    } catch (SQLException e1) {
    e1.printStackTrace();
    }
            e.printStackTrace(); 
         } finally {
          try {
         rs.close();
         sta.close();
    con.close();
    } catch (SQLException e1) {
    e1.printStackTrace();
    }
         }            
           return 0;  
         }     public static void main(String[] args) {  
      IdPass is_password = new IdPass();  
      if(is_password.init("id","pass") == 0) {
      System.out.println("ok");
      }else {
      System.out.println("error");
      }
      }  
    }
      

  7.   

    重写equals方法,你最好在重写一下hashcode方法
      

  8.   

    要重写的话2个都得一起重写吧,但首先,它没必要重写,因为String已经重写好了
      

  9.   

    重点
    另外,用equalsIgnoreCase()比较试试
      

  10.   

    你这个登陆有必要这样判定吗
    直接写在sql语句里不行吗a=a.trim();
    b=b.trim();ResultSet rs = sta.executeQuery("select  * from holder_login where id='"+a+"' and  password ='"+b+"'" ); //executeQuery  
    然后rs返回为null的话就是没这个账号或密码不对
      

  11.   

    String是已经重写好了,建议你还是看看http://www.java2000.net/viewthread.jsp?tid=2888#T3035