首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

sql查询空指针异常

2014-01-26 
在提交的servlet里出现异常 ACCESS数据库 id自动获取 password文本类型 测试id,password值能传递过来 不明

在提交的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+ " ' ";//这样也是空指针异常
-----------------------------------------------------------------
<%@page contentType= "text/html;charset=gb2312 " language= "java " import= "java.sql.* " errorPage= " "%>
<html>
<head> <title> 用户登陆 </title> </head>
<body>
用户登陆
<form method= "post " action= "/jsp/UserLogin ">
<%
if(request.getAttribute( "errInf ")!=null){
%>
<%=(String)request.getAttribute( "errInf ")%>
<%}%>
帐号 <input type= "text " name= "id ">
密码 <input type= "password " name= "password "> </p>
<input type= "submit " value= "提交 ">
<a href= "/jsp/changePassword.jsp "> 返回 </a>
</body>
</html>
---------------------------------------------------------
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         

热点排行