2013-03-10 108 views
1
public class MyBean { 

    private Connection con; 

    Set combinations; 
    public MyBean() 
    { 

     try 
     { 
      Class.forName("oracle.jdbc.OracleDriver"); 
      con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger"); 
      con.setAutoCommit(true); 

     } 
     catch(ClassNotFoundException cfe) 
     { 
      cfe.printStackTrace(); 
     } 
     catch(SQLException se) 
     { 
      se.printStackTrace(); 
     } 

    } 

     public boolean CustRegistration(String name,String username, String pwd, String address, String city,String state,long phoneno) 
    { 
     try 
      { 
     PreparedStatement st = con.prepareStatement("insert into custreg values(?,?,?,?,?,?,?)"); 
       st.setString(1,name); 
      st.setString(2,username); 
      st.setString(3,password); 
      st.setString(4,address); 
      st.setString(5,city); 
      st.setString(6,state); 
      st.setLong(7 ,phoneno); 
      int i = st.executeUpdate(); 

      if(i > 0) 
       return true; 

     } 
     catch(SQLException sety) 
     { 
      sety.printStackTrace(); 
     } 
     return false;  
    } 
public boolean check_Customer(String username,String password) 
{ 
    try 
    { 
    PreparedStatement st; 
    st = con.prepareStatement("select * from custreg where username=? and password=?"); 

     st.setString(1,username); 

     st.setString(2,password); 

     ResultSet rs = st.executeQuery(); 

     if(rs.next()) 
     { 
      st.close(); 
      return true;   
     } 

     st.close(); 
    } 
    catch(SQLException sert) 
    { 
     sert.printStackTrace(); 
    } 

    return false; 
} 

//////////////////////////////////////// //////
當過我嘗試與數據庫連接,它拋出空指針異常 堆棧跟蹤爲:的PreparedStatement拋出.NullPointerException

java.lang.NullPointerException 
    Bean.MyBean.check_Customer(MyBean.java:35)// line 35 is prepared statement which uses connection    
Servlets.LoginSubmit.doPost(LoginSubmit.java:63)//the code here is 
if(mybean.check_Customer(user,pass)) 

javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

+2

'prepareStatement'可以拋出* SQLException *(不是* NPE *)。你是否檢查過'con'不是'null'? – Maroun 2013-03-10 18:31:36

+0

您的check_Customer方法在哪裏? – Anubhab 2013-03-10 18:31:43

+1

@beigh看看你在評論中發佈的代碼。你認爲它是可讀的嗎? – Maroun 2013-03-10 18:36:07

回答

0

我假設stacktrace中的check_Customer實際上是CustRegistration方法。 在這種情況下,它意味着con域沒有被設置(它是空的)。

在構造函數中捕獲異常,然後只是打印它們。我想你有這些例外之一。也許你的類路徑中沒有oracle驅動程序?

0

很可能是您的DB is DOWN因此con爲空是因爲它無法獲得連接。

相關問題