jsp中连接数据库并取出一个字段赋值给一个字符串,然后和一个常量进行比较,输出到页面两个字符串都是“qiao”但进行equals操作,最后显示“false“(不相等)。请问这是什么原因
  数据库为SQLServer2000,字段属性为“char”....
                           多谢!!!!!!!!!!!!!!
         conn=Database.getConnection();
   stmt=conn.createStatement();
   String sql="select * from T_test where name='"+username+"'";
rs=stmt.executeQuery(sql);
while(rs.next())
{
String temp=(String)rs.getString("password");
out.println(temp);
String rrr=new String("qiao");
out.println(rrr);
if(rrr.equals(passwd))
out.println("success");
else out.println("failed");
}
rs.close();
stmt.close();
conn.close();
   }

解决方案 »

  1.   

    String temp=(String)rs.getString("password");
    out.println(temp);
    String rrr=new String("qiao");
    out.println(rrr);
    if(rrr.equals(passwd))——怎么前面的变量名是temp,后面的却是passwd。
      

  2.   

    把char改为varchar就好了……char是固定长度的,不够的会自动补上空格,所以会出错。
      

  3.   

    就是数据库的问题,本人以前也遇到过。就是把SQLServer的字段属性由“char”改为“varchar”,完了记得要把字段里的空格删掉。
      

  4.   

    原来是这个问题
    偶也曾经用char型,结果被一同事鄙视了半天。- -b
    不过偶当时存的是国内的邮政编码,char(6)。
      

  5.   

    因为char是固定长度的,不足位数时,它会自动用空格补其.
    你用下面这个方法就可以了.conn=Database.getConnection();
    stmt=conn.createStatement();
    String sql="select * from T_test where name='"+username+"'";
    rs=stmt.executeQuery(sql);
    while(rs.next())
    {
    String temp=(String)rs.getString("password");
    out.println(temp);
    String rrr=new String("qiao");
    out.println(rrr);
    if(rrr.equals(passwd.trim()))
    out.println("success");
    else out.println("failed");
    }
    rs.close();
    stmt.close();
    conn.close();
    }
      

  6.   

    因为char是固定长度的,不足位数时,它会自动用空格补其.
    你用下面这个方法就可以了.conn=Database.getConnection();
    stmt=conn.createStatement();
    String sql="select * from T_test where name='"+username+"'";
    rs=stmt.executeQuery(sql);
    while(rs.next())
    {
    String temp=(String)rs.getString("password");
    out.println(temp);
    String rrr=new String("qiao");
    out.println(rrr);
    if(rrr.equals(temp.trim()))
    out.println("success");
    else out.println("failed");
    }
    rs.close();
    stmt.close();
    conn.close();
    }