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)