package dao;
import java.sql.*;
public class Gai{ public String gai(String username,String yuanpassword,String password)
{
Connection conn=null;
Statement stmt=null;
String sql;                                       
ResultSet rs=null;
DB db=new DB();
conn=db.getConn();
try{
stmt=conn.createStatement();
sql="select * from yonghu where username='"+username+"'";
        rs=stmt.executeQuery(sql);
        while(rs.next())                  //如果查到了该用户,则再把相应的密码取出来
        {   
            String temp=rs.getString("password");
         if(yuanpassword.equals(temp))
         {String sql2="update yonghu set password='"+password+"' where username='"+username+"'";
          stmt.executeUpdate(sql2);
          return "success";        //只有用户名和密码都正确时才会返success
         }
         else 
          return "wrong";
        }
        }
catch(SQLException e2)
        {
            System.out.println("数据库存在异常!查操作时出现错误");
            System.out.println(e2.toString());
        }
finally
        {
            try
            {
                if(rs != null) rs.close();
                if(stmt != null) stmt.close();
                if(conn!= null) conn.close();
            }
         catch(SQLException e)
         {
             System.out.println("数据库存在异常!查操作时出现错误");
         }            
        }
return "wrong";
}
}
返回servlet的时候总是返回wrong,我打开数据库输入个正确的帐号和密码还是返回wrong,所以我判断是我返回success的那部分有错误导致没有被执行 但是看了许久无果 求大神点睛=。=

解决方案 »

  1.   

    也不是sql的错误 不然我的控制台会有提示 也不是表的问题 表的结构我反复检查过了实在困惑=.=
      

  2.   

    String temp=rs.getString("password");
    System.out.println(temp);  
    if(yuanpassword.equals(temp))
    能得到这个对象么?你先看一下
      

  3.   

    我建议你还是把这种public class Gai{}写成单独的类,里面有增删查该的方法,有返回值就行
    要用哪个方法直接根据 (类.方法名)调用。这样可能找错也好找层次更清楚