【问题标题】:HTTP Status 500 – Internal Server Error in EclipseHTTP 状态 500 - Eclipse 中的内部服务器错误
【发布时间】:2021-06-14 22:56:36
【问题描述】:

我做了一个动态项目并实现了删除 crud,在编码之后,我在控制台第一次插入时遇到了这个错误,读取 crud 工作正常。实施 delete crud 后,我遇到了这个错误,我被困在这部分

deleteItem 方法

在Item类中实现了一个删除方法

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代码

下面是我调用删除项方法的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)

我的完整物品类别如下

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   ;
    }
        
        
    
    
    
    
    
    
}

【问题讨论】:

  • 在哪里你把问题中列出的deleteItem 方法放在了哪里?您列出的内容缺乏上下文。你也有Items 类吗?
  • @nitind 它的项目对象不是一个类
  • 我已经将该方法放在项目类中,以便我在 jsp 中调用它
  • 我们看不到。告诉我们为什么消息是错误的。
  • @nitind 我在最后一部分提到了错误

标签: java eclipse jsp server


【解决方案1】:

上面写着The method deleteItem(String) is undefined for the type Item,你能再试试构建项目吗

【讨论】:

  • 重新构建是什么意思
  • 我没有足够的代表发表评论,所以不得不把它写成 ans,在 eclipse 中选择项目,构建项目。如果是 maven 项目,你可以尝试全新安装
  • 在我的日食中,它是一个动态项目,并且在该项目中勾选了自动构建
  • 自动生成的版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 2015-03-24
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多