在提交的servlet里出现异常
ACCESS数据库
id自动获取
password文本类型
测试id,password值能传递过来
不明白为什么
String query= "select * from customer where password= "+pwd; //这样是空指针异常
String query= "select * from customer where id= ' "+id+ " ' and password= ' "+pwd+ " ' ";//这样也是空指针异常
-----------------------------------------------------------------
用户登陆用户登陆
if(request.getAttribute( "errInf ")!=null){
%>
帐号
密码
---------------------------------------------------------
UserLogin
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import bean.DBClass;
import bean.StrClass;
import bean.UserInf;
public class UserLogin extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
HttpSession session=request.getSession();
String toJsp= "/hasLogin.jsp ";
if(session.getAttribute( "userId ")==null){
String id=request.getParameter( "id ");
String pwd=request.getParameter( "pwd "); //String query= "select * from customer where id= "+id; 这样就可以正常登陆
//String query= "select * from customer where password= "+pwd; 这样是空指针异常
String query= "select * from customer where id= ' "+id+ " ' and password= ' "+pwd+ " ' ";//这样也是空指针异常
DBClass db=new DBClass();
db.connect();
ResultSet resultset=db.executeQuery(query);
try{
if(resultset.next()){
session.setAttribute( "userid ",id);
toJsp= "/loginSuccess.jsp ";
}
else{
toJsp= "/userLogin.jsp ";
request.setAttribute( "errIn ", "*密码与帐号不匹配 ");
}
}
catch(SQLException sqle){
System.err.println( "Erro width connection: "+sqle);
}
db.closeConnection();
}
RequestDispatcher dispatcher=request.getRequestDispatcher(toJsp);
dispatcher.forward(request,response);
}
}
------空指针异常解决方法--------------------------------------------------------
String query= "select * from customer where password= "+pwd; //这样是空指针异常
String query= "select * from customer where id= ' "+id+ " ' and password= ' "+pwd+ " ' ";//这样也是空
这样肯定不会空指针异常的!
out.print(result);//为什么会是null呢
可能是ResultSet resultset=db.executeQuery(query);有异常了。
------解决方法--------------------------------------------------------
if(session.getAttribute( "userId ")==null){
这句当中的'userID'你在哪setAttribute了?
------解决方法--------------------------------------------------------
DBClass db=new DBClass();
db.connect();
ResultSet resultset=db.executeQuery(query);
这个需要看看 DBClass 这个类里面的 executeQuery 方法,是怎么写的了。
有可能这个方法,把Exception给截获了,没有抛出来。
因为出错了以后,就返回NULL