yiqianyan 2017-03-26 14:12 采纳率: 100%
浏览 1169
已采纳

空指针异常,jsp+servlet+jdbc

login.jsp:

 <form name="formName" method="post" action="servlet/AdminVerify">
    <tr>
        <td width="18%" height="30">
            <div align="center">用户</div>
        </td>
        <td width="50%" height="30"><input name="name" type="text"></td>
        <!-- 宽度规定,然后rowspan,则该列都合并,其他的行少出现一个td的使用 -->
        <td width="32%" rowspan="4">
            <!-- input里面可以有斜杠,也可无 --> <!-- 注意"image"类型的用法,必须有图片src参数。它相当于一个按钮,
                                                                     所以可以有onClick,且注意return及分号不能少,实际上传递的false值无意义--> <input
            type="image" name="imageField" src="images/dl.gif"
            onClick="return check();">
        </td>
    </tr>
    <tr>
        <td height="30">
            <div align="center">密码</div>
        </td>
        <td height="30"><input name="password" type="password"></td>
    </tr>
    <tr>
        <td height="30">
            <div align="center">权限</div>
        </td>
        <td height="30">
            <!-- 下拉列表,文本内容会显示 --> <select name="st">
                <option value="管理员">管理员</option>
                <option value="客户">客户</option>
        </select>
        </td>
    </tr>
    <!-- table中的空白行和列可以有center,left,right属性的作用 -->
    <tr>
        <!-- 虽未空白行,但是td不能省略 -->
        <td height="30"></td>
        <td height="30"></td>
    </tr>
</form>


web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>hotel2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>AdminVerify</servlet-name>
    <servlet-class>servlet.AdminVerify</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AdminVerify</servlet-name>
    <url-pattern>/servlet/AdminVerify</url-pattern>
  </servlet-mapping>
</web-app>

AdminVerify.java:

 package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.Service;

public class AdminVerify extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();

        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String st = request.getParameter("st");

        Service service=new Service();
        ResultSet rs=service.getResult(name,password,st);
        try {//rs出现的地方都需要try-catch
            if (!rs.next()) {
                out.print("<script>alert('你输入的用户不存在或密码错误,请重新登录!');window.history.go(-1);</script>");
            } else {
                //
                //
                request.getSession().setAttribute("name", request.getParameter("name"));
                if (request.getParameter("st").equals("客户")) {
                    //st一样赋值给session
                    request.getSession().setAttribute("st", "客户");
                } else {
                    //password这里不必要放入session
                    request.getSession().setAttribute("st", rs.getString("user_type"));
                }
                response.sendRedirect(request.getContextPath()+"/main.jsp");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

Service.java:

 package service;

import java.sql.*;
import java.util.*;

import db.DBUtil;

public class Service {
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    private String sql;



    public ResultSet getResult(String name, String password, String st) {
        //这一句放到方法外面出现错误
        conn=DBUtil.getConnection();
        if (st.equals("客户")) {
            sql = "select * from customr where customer_name=name and customer_password=password";
        } else {
            sql = "select * from users where user_name=name and user_password=password";
        }
        try {
            // jsp没try不会报错,容易错误
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

        } catch (Exception e) {
            e.printStackTrace();
        }
        // 
        return rs;
    }
}

java.lang.NullPointerException
at servlet.AdminVerify.doPost(AdminVerify.java:30)
在if(!rs,next())处

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'name' in 'where clause'

  • 写回答

4条回答 默认 最新

  • ClownsAndTears 2017-03-27 03:42
    关注

    最好使用占位符,按照我的猜测,你这个sql语法也许有错误。
    比如select * from customr where customer_name=name and customer_password=password;
    也许sql会在你的表里寻找 customer_name、name、customer_password、password这四个字段。
    说到这里可能你就明白了。先说我的解决方法吧。
    1.sql = "select * from customr where customer_name="+name+" and customer_password="+password;
    方法1比较简单,直接拼接就好了。
    2.sql = "select * from customr where customer_name=? and customer_password=?";
    方法2是占位符,需要设置。
    // 这里获取链接
    Connection con = JdbcUtil3.getConnection();
    // 然后编译
    PreparedStatement ps = con.prepareStatement(sql);
    // 填充占位符
    ps.setString(1, name);
    ps.setString(2, password);
    .....
    然后执行方法。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Java新项目启动报错
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值
  • ¥20 数据排序,可选择排序方向
  • ¥15 修改一下代码,考虑进程到达时间不同的情况
  • ¥15 帮我看看这是个啥题,带解题过程和结果,条件如下FCF = 290471.33 g1 = 15% r = 8% g2 = 4% n = 5
  • ¥15 edem模拟颗粒不显示或者生成失败
  • ¥15 Python代码编写