私は誰かが私はこのエラーにサーブレットコードのエラー:HTTP 500内部サーバーエラー
のデバッグを助けてもらえ
を私は次のコードを実行しようとしているが、それは常に「HTTP 500内部サーバーエラー」になりサーブレットと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