我试图运行下面的代码,但它总是导致“HTTP 500内部服务器错误”servlet代码错误:HTTP 500内部服务器错误
有人能帮助我调试这个错误
我刚开始学习Servlets和JSP ..所以请原谅,如果我错过任何问题的细节。展望在MySQL数据库中的错误日志,我发现下面的条目:
Aborted connection 44 to db: 'sakila' user: 'root' host: 'localhost' (Got an error reading communication packets)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out=response.getWriter();
final String DB_URL="jdbc:mysql://localhost:3306/sakila";
final String user="root";
final String password="pass1234";
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
out.println("Cannot load driver");
}
Connection conn=null;
try {
conn=DriverManager.getConnection(DB_URL,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
out.println("Cannot Connect to Database");
}
//out.print("Connected to Database");
out.println("<html>");
out.println("<body>");
String str= "SELECT actor_id, first_name last_name FROM temp where actor_id='1';";
try {
ResultSet rs=stmt.executeQuery(str);
while(rs.next()){
out.println(rs.getString("actor_id"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
try {
ResultSet rs;
rs = stmt.executeQuery(str);
while(rs.next()){
int i=rs.getInt("actor_id");
String fn= rs.getString("first_name");
String ln=rs.getString("last_name");
out.print(i+"::");
out.print(fn+"::");
out.print(ln+"::");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
out.print("hkshfdkhfakfshdkha");
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("</body>");
out.println("</html>");
}
不要忽略和忽略异常。在不终止流程流程的情况下打印异常仍然*忽略异常。另外,如果没有**查看服务器日志**,那里写入了实际错误,则无法确切地说明导致错误的原因。我们可以猜测(其他人在下面的答案中),但实际上有太多的可能性使其有用。 – Andreas
我想到了这个问题。我忘了实例化Statement对象:stmt = conn.createStatement(); – avg998877
感谢所有的帮助 – avg998877