令牌“}”上的 Eclipse 语法错误,删除此令牌和语法错误,插入“}”以完成 ClassBody

Eclipse Syntax error on token "}", delete this token and Syntax error, insert "}" to complete ClassBody

我正在尝试为书店店员制作一个练习程序,允许店员在其数据库中添加、删除、编辑和搜索书籍。我几乎完成了整个程序,但是我遇到了 2 个错误。总共有 234 行代码,所以我会尽量将它缩短到相关部分,以便那些愿意帮助我的人更容易理解。我将 Eclipse 与 JDE 和 JDK 10 一起使用。据我所知,Eclipse 项目是使用 JavaSE-10 执行环境启动的。以下是导致错误的 2 种方法。

public class Bookstore {

    public static void main(String[] args) {
        try(
                //Creating table connection and statement
                Connection conn = DriverManager.getConnection("***********",
                        "****", "*********"); //Please note that I blocked out the actual connection information here

                Statement stmt = conn.createStatement();
                ){

            Scanner input = new Scanner(System.in);
            int selection = 0;

            //Menu for action selection and user input
            while(selection != 5) {
                System.out.println("Please enter the number corresponding to the action you would like to take:\n"
                        + "1. Enter book\n"
                        + "2. Update book\n"
                        + "3. Delete book\n"
                        + "4. Search books\n"
                        + "5. Exit");
                selection = input.nextInt();

                //Selection sorting
                if(selection == 1) {
                    //Collecting book information
                    System.out.println("Please enter the Title of the book you would like to put into the system: ");
                    String title = input.next();
                    System.out.println("Please enter the Author of said book: ");
                    String author = input.next();
                    System.out.println("Please enter the number of said book currently in stock: ");
                    int qty = input.nextInt();

                    //Sending info to the addBook method
                    addBook(title, author, qty, stmt);
                } else if(selection == 2) {
                    //Collecting book information
                    System.out.println("Please enter the id of the book you would like to update: ");
                    int id = input.nextInt();

                    //Sending info to the updateBook method
                    updateBook(id, stmt);
                } else if(selection == 3) {
                    //Collecting book information
                    System.out.print("Please enter the id of the book you would like to delete from the system: ");
                    int id = input.nextInt();

                    //Sending info to deleteBook method
                    deleteBook(id, stmt);
                } else if(selection == 4) {
                    searchStore(stmt);
                } else if(selection == 5) {
                    System.out.println("Goodbye");
                    input.close();
                } else { //Invalid entry handler
                    System.out.println("Sorry, that isn't a valid selection.");
                }
            }

        } catch(SQLException ex) {
            ex.printStackTrace();
        }
    }
} //This is the line giving me the error "Syntax error on token "}", delete this token"

现在我已经对这段代码底部的错误进行了一些研究。据我所知,我没有遗漏任何括号,并且没有变量或在 class 之外创建的任何内容会导致此错误。我能够找到的唯一其他解决方案是 "Eclipse is just being weird".

我的第二个错误来自这段代码:

public static void resultSetPrinter(ResultSet rset) {
    while(rset.next()) {
        String title = rset.getString("Title");
        String author = rset.getString("Author");
        int qty = rset.getInt("qty");
        System.out.println("Title: " + title + "\nAuthor: " + author + "\nNumber in stock: " + qty + "\n\n");
    }
    if(rset == null) {
        System.out.println("No records for the entry could be found.");
    }
} //This is the line giving me the "Syntax error, insert "}" to complete ClassBody" error

我还对这个块底部的错误进行了一些研究,当我按照要求删除括号时,错误只会跳到这个方法之前的方法。我没有在 class 中包含其他 4 种方法来尝试通过所有这些代码减少 运行 的麻烦,因为它们没有给我错误。在这一点上,任何帮助将不胜感激,我完全被难住了。

主要感谢 Elliott Frisch,我找到了答案。本质上,我需要将我所有的方法以 Bookstore 的名称放入我的主 class 中。我将 } 移到了程序的末尾,并为每个方法添加了 try catch 语句。例如,我将问题中的最后一个代码块更改为:

public static void resultSetPrinter(ResultSet rset) {
    try {
        if(rset.next()) {
            while(rset.next()) {
                String title = rset.getString("Title");
                String author = rset.getString("Author");
                int qty = rset.getInt("Qty");
                System.out.println("Title: " + title + "\nAuthor: " + author + "\nNumber in stock: " + qty + "\n");
            }
        } else {
            System.out.println("No records for the entry could be found.\n");
        }
    } catch(SQLException ex) {
        ex.printStackTrace();
    }
}

您还会注意到我添加了一个 if else 语句来检查 ResultSet rset 是否为空,如果不是,我将照常进行,如果是,我打印一条简单的消息让用户知道什么也没找到。

感谢 Elliott Frisch 和 Marco13 的协助。