Apache状态500:空指针异常

时间:2015-01-28 14:58:41

标签: java html apache servlets connection

我正在尝试使用servlet将HTML数据发送到我的数据库。请帮帮我吧!

这是我的doGet功能

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Student st=new Student();
    st.setId(Integer.parseInt(request.getParameter("id")));
    st.setName(request.getParameter("name"));
    DBConnection db= new DBConnection();
    db.addStudent(st);
}

这是我的addStudent函数

public class DBConnection {

private static final String driverName = "com.mysql.jdbc.Driver";
static final String URL = "jdbc:mysql://localhost:3306/Test1";
private static final String username = "root";
private static final String password = "root";

public static Connection getConnection() {
    System.out.println("HEre");
    Connection connection = null;
    try {
        Class.forName(driverName);
        connection = DriverManager.getConnection(URL, username, password);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return connection;
}

public void addStudent(Student st) {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        System.out.println(st.getId() + st.getName());
        String Query = "INSERT INTO DETAILS VALUES ( ? , ? , ? )";
        connection = getConnection();
        preparedStatement = connection.prepareStatement(Query);
        preparedStatement.setInt(1, st.getId());
        preparedStatement.setString(2, st.getName());
        preparedStatement.setBlob(3, (Blob) null);
        preparedStatement.executeUpdate();
    } 

这是我点击提交时获得的例外:

type Exception report

message description The server encountered an internal error that prevented it from fulfilling this request.

exception   java.lang.NullPointerException
    DataAccessObject.DBConnection.addStudent(DBConnection.java:42)
    ActionObject.Input.doGet(Input.java:36)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

这些是我的apache服务器日志:

127.0.0.1 - - [28/Jan/2015:19:13:02 +0530] "GET /Test1/Input?name=&id=&mydropdown=Coding&mydropdown=Coding&marks=&submitbtn=Submit HTTP/1.1" 500 1478
127.0.0.1 - - [28/Jan/2015:20:09:15 +0530] "GET / HTTP/1.1" 200 11243

1 个答案:

答案 0 :(得分:1)

将mysql-connector-java.jar添加到项目中:

enter image description here

然后在项目的java构建路径中添加jar:

enter image description here