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)
'prepareStatement'可以拋出* SQLException *(不是* NPE *)。你是否檢查過'con'不是'null'? – Maroun 2013-03-10 18:31:36
您的check_Customer方法在哪裏? – Anubhab 2013-03-10 18:31:43
@beigh看看你在評論中發佈的代碼。你認爲它是可讀的嗎? – Maroun 2013-03-10 18:36:07