我正在尝试为书店店员制作一个练习程序,允许该店员在其数据库中添加,删除,编辑和搜索书籍。我几乎制作了整个程序,但是我陷入了2个错误。它总共有234行代码,因此我将尝试将其缩短为相关部分,以使那些愿意为我提供帮助的人更加轻松。我将Eclipse与JDE和JDK 10一起使用。据我所知,Eclipse项目是使用JavaSE-10执行环境启动的。以下是导致错误的两种方法。
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"
现在,我已经在此代码块底部针对错误进行了一些研究。据我所知,我没有遗漏任何括号,并且在类之外没有任何变量或任何正在创建的会导致此错误的内容。我唯一能找到的其他解决方案是“ Eclipse只是很奇怪”。
我的第二个错误来自此代码块:
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
我也对该块底部的错误进行了一些研究,当我按要求卸下支架时,该错误就跳到了此方法之前的方法。我没有在该类中包括其他4种方法来尝试减少运行所有这些代码的麻烦,因为它们没有给我错误。此时,我将不胜感激,将不胜感激。
答案 0 :(得分:0)
主要感谢Elliott Frisch,我找到了答案。本质上,我需要将所有方法都以Bookstore的名称放入主类。我将}
移到程序的末尾,并为每种方法添加了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的协助。