Apache 状态 500:空指针异常
Apache status 500 : Null pointer exception
我正在尝试使用 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
将 mysql-connector-java.jar 添加到您的项目中:
然后在项目的java构建路径中添加jar:
我正在尝试使用 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
将 mysql-connector-java.jar 添加到您的项目中:
然后在项目的java构建路径中添加jar: