我試圖運行下面的代碼,但它總是導致「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