最好从while之后用System.out.print跟踪,看看取到数据库中的数据没有很可能是没有取到数据而抛出空指针异常

解决方案 »

  1.   

    public void checkPassword(User user) throws ClassNotFoundException,SQLException,Exception //check the password
       {
         boolean Loggedin=true;
     
         try{
         connect();
         //con.setAutoCommit(false);
          Statement s = con.createStatement();System.out.println("creat");
         //creat不能被输出
          ResultSet rs = s.executeQuery("SELECT * FROM UserDB");System.out.println("select");
            while(rs.next())
            { 
              String pn = rs.getString(2);System.out.println(pn);
              String pw = rs.getString(2);System.out.println(pw);
              if(((user.getPhoneNumber()).equals(pn))&&((user.getPassword()).equals(pw)))
                 {user.setIsLoggedin(Loggedin);return;}
            }
    user.setIsLoggedin(!Loggedin);
         rs.close();
         s.close();
         //con.commit();
         disconnect();
     }catch(SQLException cnfe){
      error="SQLException:Could not connect to database.";
      throw new SQLException(error);
     }catch(Exception e){
      error="Exception:An unkown error occurred while connecting to database.";
      throw new Exception(error);
     }
        }CONNECT函数:
    public void connect() throws ClassNotFoundException,SQLException,Exception //connect to database
       { System.out.println("connect");
         try{
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
          Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=UserDB","sa","");  }catch(ClassNotFoundException cnfe){
      error="ClassNotFoundException:Could not locate DB driver.";
      throw new ClassNotFoundException(error);
     }catch(SQLException cnfe){
      error="SQLException:Could not connect to database.";
      throw new SQLException(error);
     }catch(Exception e){
      error="Exception:An unkown error occurred while connecting to database.";
      throw new Exception(error);
     } }
    大家请帮我看看这个BEAN,在CONNECT里面完成数据库的连接,我在con.setAutoCommit(false);和Statement s = con.createStatement();之间插了一句输出语句,发现不能输出,但是当注释掉con.setAutoCommit(false);以后就可以输出,然后在Statement s = con.createStatement()之后加了一句输出语句,发现也不能输出,非常地不明白,希望高手指教
      

  2.   

    con.setAutoCommit(false);
    你再用这句话的时候要了解它的意义呀!在数据库第一次建立于数据库连接的时候,在缺省的情况下,连接是自动提交模式的,为了获得更好的性能,可以通过调用代布尔值false参数的Connection类的setAutoCommit()方法来关闭自动提交的功能,但是一旦关闭了自动提交功能,我们就必须通过调用Connection类的commitv()和rollback()方法来人工的方式对事务管理。所以在用这个的时候必须注意!
      

  3.   

    有点明白了,但是我还是不大明白的就是,如果我去掉con.setAutoCommit(false);
    的注释,那个"creat"为什么还是不能被输出
      

  4.   

    还有很奇怪的:大家注意看我第一次贴的那个SERVLET:LOGIN.JAVA
    如果我在TRY函数块里一开始将TARGET赋值,比如赋WELCOME.JSP就正常地连过去
    这就说明在我那个SERVLET中经过几轮判断之后,其实TARGET没有被赋值,但是我想不通的就是如果什么条件都不满足的话,至少得被赋给ERROR.JSP吧
    希望大家擦亮眼睛告诉我问题所在!谢谢拉~~~~~~~~~~
      

  5.   

    大家有发现任何问题吗,我实在是毫无头绪,涕零求助ING~~~~~~~~~~
      

  6.   

    你使用了null对象,也就是说有可能从JSP页面上取数据的时候没有取到,而你又在使用这个null对象
      

  7.   

    谢谢大家,已经解决了
    感谢kevinliuu和Arias