我试图为书店职员制作一个实践程序,允许书记员在其数据库中添加、删除、编辑和搜索书籍。我已经做了整个程序,但我被困在两个错误。总共有234行代码,所以我会尽量把它缩短到相关部分,让那些愿意帮助我的人更容易。据我所知,Eclipse与JDE和JDK 10一起使用Eclipse,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种方法,因为它们不会给我带来错误,所以我没有尝试减少运行所有这些代码所带来的麻烦。在这一点上,任何帮助都将是非常感谢的,我完全感到困惑。
发布于 2018-10-08 23:12:57
主要感谢埃利奥特·弗里斯,我找到了答案。本质上,我需要将我的所有方法以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 rset语句,以检查rset是否为空,如果不是,则按正常方式进行,如果是,则打印一条简单的消息,让用户知道什么都没有找到。
谢谢你们两位埃利奥特·弗里斯和Marco13的支持。
https://stackoverflow.com/questions/52709378
复制相似问题