HTTP 状态 500 – Eclipse 中的内部服务器错误
HTTP Status 500 – Internal Server Error in Eclipse
我做了一个动态项目并实施了删除 crud,在编码之后,我在我的控制台中遇到了这个错误,第一次插入和读取 crud 工作正常。实施 delete crud 后我遇到了这个错误,我被困在这部分
deleteItem method
在项目class
中实现了删除方法
public String deleteItem(String itemID) {
String output = "";
try {
Connection con = connect();
if(con == null) {
return "Error while connecting to the database for deleting.";
}
//create a prepared statement
String query = "delete from items where itemID=?";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, Integer.parseInt(itemID));
// execute the statement
preparedStmt.execute();
con.close();
output = "Deleted successfully";
}
catch (Exception e)
{
output = "Error while deleting the item.";
System.err.println(e.getMessage());
}
return output ;
}
jsp code
下面是我的 jsp 调用删除项目方法的代码
<%
//Delete item----------------------------------
if (request.getParameter("itemID") != null)
{
Item Items = new Item();
String stsMsg = Items.deleteItem(request.getParameter("itemID"));
session.setAttribute("statusMsg", stsMsg);
//deleteItem(request.getParameter("itemID"));
}
%>
错误
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Unable to compile class for JSP:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [59] in the jsp file: [/itemps.jsp]
The method deleteItem(String) is undefined for the type Item
56: if (request.getParameter("itemID") != null)
57: {
58: Item Items = new Item();
59: String stsMsg = Items.deleteItem(request.getParameter("itemID"));
60: session.setAttribute("statusMsg", stsMsg);
61:
62: //deleteItem(request.getParameter("itemID"));
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:482)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:392)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:362)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:346)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:605)
我的完整项目class如下
package com;
import java.sql.*;
public class Item {
public Connection connect() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/item",
"root", null);
//For testing
System.out.print("Successfully connected");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
public String insertItem(String code ,String name ,String price ,String desc) {
String output = "";
try
{
Connection con = connect();
if(con == null ) {
return "Error While connecting to the database ";
}
String query = "insert into item (itemID,itemCode,itemName,itemPrice,itemDesc)"
+ "values (?,?,?,?,?)";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, 0);
preparedStmt.setString(2, code);
preparedStmt.setString(3, name);
preparedStmt.setDouble(4, Double.parseDouble(price));
preparedStmt.setString(5, desc);
preparedStmt.execute();
con.close();
output = "Inserted successfully";
}
catch (Exception e)
{
output = "Error while inserting";
System.err.println(e.getMessage());
}
return output ;
}
public String readItems() {
String output = "";
try {
Connection con = connect();
if (con == null) {
return "Error While conecting to the database for reading ";
}
//preapre the html table to be displayed
output = "<table boder= '1'><tr><th>Item Code</th>"
+"<th>Item Name</th><th>Item Price</th>"
+ "<th>Item Description</th>"
+ "<th>Update</th><th>Remove</th></tr>";
String query = "select * from item";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// iterate through the rows in the result set
while(rs.next()) {
String itemID = Integer.toString(rs.getInt("itemID"));
String itemCode = rs.getString("itemCode");
String itemName = rs.getString("itemName");
String itemPrice = Double.toString(rs.getDouble("itemPrice"));
String itemDesc = rs.getString("itemDesc");
// Add a row into the html table
output += "<tr><td>" + itemCode + "</td>";
output += "<td>" + itemName + "</td>";
output += "<td>" + itemPrice + "</td>";
output += "<td>" + itemDesc + "</td>";
// buttons
output += "<td><input name='btnUpdate' "
+ " type='button' value='Update'></td>"
+ "<td><form method='post' action='items.jsp'>"
+ "<input name='btnRemove' "
+ " type='submit' value='Remove'>"
+ "<input name='itemID' type='hidden' "
+ " value='" + itemID + "'>" + "</form></td></tr>";
}
con.close();
// Complete the html table
output += "</table>";
}
catch (Exception e)
{
output = "Error while reading the items.";
System.err.println(e.getMessage());
}
return output;
}
public String deleteItem(String itemID) {
String output = "";
try {
Connection con = connect();
if(con == null) {
return "Error while connecting to the database for deleting.";
}
//create a prepared statement
String query = "delete from items where itemID=?";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, Integer.parseInt(itemID));
// execute the statement
preparedStmt.execute();
con.close();
output = "Deleted successfully";
}
catch (Exception e)
{
output = "Error while deleting the item.";
System.err.println(e.getMessage());
}
return output ;
}
}
它说 The method deleteItem(String) is undefined for the type Item
,你能再试一次构建项目吗
我做了一个动态项目并实施了删除 crud,在编码之后,我在我的控制台中遇到了这个错误,第一次插入和读取 crud 工作正常。实施 delete crud 后我遇到了这个错误,我被困在这部分
deleteItem method
在项目class
中实现了删除方法public String deleteItem(String itemID) {
String output = "";
try {
Connection con = connect();
if(con == null) {
return "Error while connecting to the database for deleting.";
}
//create a prepared statement
String query = "delete from items where itemID=?";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, Integer.parseInt(itemID));
// execute the statement
preparedStmt.execute();
con.close();
output = "Deleted successfully";
}
catch (Exception e)
{
output = "Error while deleting the item.";
System.err.println(e.getMessage());
}
return output ;
}
jsp code
下面是我的 jsp 调用删除项目方法的代码
<%
//Delete item----------------------------------
if (request.getParameter("itemID") != null)
{
Item Items = new Item();
String stsMsg = Items.deleteItem(request.getParameter("itemID"));
session.setAttribute("statusMsg", stsMsg);
//deleteItem(request.getParameter("itemID"));
}
%>
错误
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Unable to compile class for JSP:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [59] in the jsp file: [/itemps.jsp]
The method deleteItem(String) is undefined for the type Item
56: if (request.getParameter("itemID") != null)
57: {
58: Item Items = new Item();
59: String stsMsg = Items.deleteItem(request.getParameter("itemID"));
60: session.setAttribute("statusMsg", stsMsg);
61:
62: //deleteItem(request.getParameter("itemID"));
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:482)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:392)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:362)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:346)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:605)
我的完整项目class如下
package com;
import java.sql.*;
public class Item {
public Connection connect() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/item",
"root", null);
//For testing
System.out.print("Successfully connected");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
public String insertItem(String code ,String name ,String price ,String desc) {
String output = "";
try
{
Connection con = connect();
if(con == null ) {
return "Error While connecting to the database ";
}
String query = "insert into item (itemID,itemCode,itemName,itemPrice,itemDesc)"
+ "values (?,?,?,?,?)";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, 0);
preparedStmt.setString(2, code);
preparedStmt.setString(3, name);
preparedStmt.setDouble(4, Double.parseDouble(price));
preparedStmt.setString(5, desc);
preparedStmt.execute();
con.close();
output = "Inserted successfully";
}
catch (Exception e)
{
output = "Error while inserting";
System.err.println(e.getMessage());
}
return output ;
}
public String readItems() {
String output = "";
try {
Connection con = connect();
if (con == null) {
return "Error While conecting to the database for reading ";
}
//preapre the html table to be displayed
output = "<table boder= '1'><tr><th>Item Code</th>"
+"<th>Item Name</th><th>Item Price</th>"
+ "<th>Item Description</th>"
+ "<th>Update</th><th>Remove</th></tr>";
String query = "select * from item";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// iterate through the rows in the result set
while(rs.next()) {
String itemID = Integer.toString(rs.getInt("itemID"));
String itemCode = rs.getString("itemCode");
String itemName = rs.getString("itemName");
String itemPrice = Double.toString(rs.getDouble("itemPrice"));
String itemDesc = rs.getString("itemDesc");
// Add a row into the html table
output += "<tr><td>" + itemCode + "</td>";
output += "<td>" + itemName + "</td>";
output += "<td>" + itemPrice + "</td>";
output += "<td>" + itemDesc + "</td>";
// buttons
output += "<td><input name='btnUpdate' "
+ " type='button' value='Update'></td>"
+ "<td><form method='post' action='items.jsp'>"
+ "<input name='btnRemove' "
+ " type='submit' value='Remove'>"
+ "<input name='itemID' type='hidden' "
+ " value='" + itemID + "'>" + "</form></td></tr>";
}
con.close();
// Complete the html table
output += "</table>";
}
catch (Exception e)
{
output = "Error while reading the items.";
System.err.println(e.getMessage());
}
return output;
}
public String deleteItem(String itemID) {
String output = "";
try {
Connection con = connect();
if(con == null) {
return "Error while connecting to the database for deleting.";
}
//create a prepared statement
String query = "delete from items where itemID=?";
PreparedStatement preparedStmt = con.prepareStatement(query);
// binding values
preparedStmt.setInt(1, Integer.parseInt(itemID));
// execute the statement
preparedStmt.execute();
con.close();
output = "Deleted successfully";
}
catch (Exception e)
{
output = "Error while deleting the item.";
System.err.println(e.getMessage());
}
return output ;
}
}
它说 The method deleteItem(String) is undefined for the type Item
,你能再试一次构建项目吗