java吧 关注:1,230,963贴子:12,692,522
  • 7回复贴,共1

这个servlet在操作数据库验证的时候为什么报空指针

只看楼主收藏回复

package LoginServlet;import org.hz.dbc.*;import java.io.*;import java.sql.*;
import javax.servlet.*;import javax.servlet.http.*;
public class Check extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
PreparedStatement ps = null;
ResultSet rs = null;
request.setCharacterEncoding("GBK");
response.setContentType("text/html");
//获取账号密码
String name = request.getParameter("name");
String pass = request.getParameter("password");
//获取数据库操作
databaseConnection dbc = new databaseConnection();
String sql = "select UPASS from users where UNAME='?'";
try {
ps=dbc.getConnection().prepareStatement(sql);
ps.setString(1, name);
rs=ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs.next()){
//保存密码,与输入的密码进行匹配,
String p = rs.getString(1);
if(pass.equals(p)){
request.setAttribute("name", "name");
request.getRequestDispatcher("wel").forward(request, response);
}}else{
//不匹配则跳回
response.sendRedirect("Login.java");}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try{
dbc.getConnection().close();}catch(Exception e){e.printStackTrace();}}}
}


1楼2013-05-22 12:09回复
    在if(rs.next())这个地方报异常,空指针


    2楼2013-05-22 12:10
    回复
      如果在这句报空指针,就只能是rs为空吧?


      来自Android客户端3楼2013-05-22 13:26
      收起回复
        占位符不要用引号


        IP属地:广东4楼2013-05-22 13:51
        收起回复
          说对了,经过我测试确实是rs为空,但是不知道为什么为空


          来自iPhone客户端5楼2013-05-22 18:22
          回复