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;
}

///////////////////////////////////////
每当我尝试与数据库连接它会抛出 NULL POINTER EXCEPTION 堆栈跟踪是:

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.爪哇:393)

4

2 回答 2

0

很可能您的DB is DOWNsocon为空,因为它无法建立连接。

于 2013-03-10T18:37:41.430 回答
0

我假设堆栈跟踪中的 check_Customer 实际上是 CustRegistration 方法。在这种情况下,这意味着未设置 con 字段(它为空)。

在构造函数中,您正在捕获异常,然后打印它们。我认为你有这些例外之一。也许您的类路径中没有 oracle 驱动程序?

于 2013-03-10T18:35:45.297 回答