簡體   English   中英

HTTP 狀態 500 – Eclipse 中的內部服務器錯誤

[英]HTTP Status 500 – Internal Server Error in Eclipse

我做了一個動態項目並實現了刪除 crud,編碼后,我在控制台第一次插入時遇到了這個錯誤,讀取 crud 工作正常。 實施 delete crud 后,我遇到了這個錯誤,我被困在這部分

deleteItem 方法

在項目 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代碼

下面是我的 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 ,您可以嘗試再次構建項目嗎

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM