首页 > 解决方案 > 在java中使用数据库处理程序执行操作的问题

问题描述

我正在努力调试为什么我的程序在特定行停止,我试图了解它是否与创建表或语句有关,但控制台没有给我任何错误,并且程序在运行此功能后仍然卡住:

 @FXML
    private void loadIssueOperation(ActionEvent event) {
        String memberID = memberIdInput.getText();
        String bookID = bookIdInput.getText();

        Alert confAlert = new Alert(Alert.AlertType.CONFIRMATION);
        confAlert.setTitle("Confirm New Opperation");
        confAlert.setHeaderText(null);
//TASK1 
        confAlert.setContentText("Are you sure about issue the book "+bookName.getText()+
                "\n to "+memberName.getText()+"?");

        Optional<ButtonType> response = confAlert.showAndWait();
        if(response.get()==ButtonType.OK){
            String insertStr = "INSERT INTO ISSUE(memberID,bookID) VALUES("
                    + "'" + memberID + "',"
                    + "'" + bookID + "')";
            String updateStr = "UPDATE BOOK SET isAvail = false WHERE id = '"+bookID+"'";
            System.out.println(insertStr+" and "+updateStr);

            if(dataBaseHandler.execAction(insertStr) && dataBaseHandler.execAction(updateStr)){
                    Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
                    successAlert.setTitle("Success");
                    successAlert.setHeaderText(null);
                    successAlert.setContentText("Complete");
                    successAlert.showAndWait();
                }else{
                    Alert failedAlert = new Alert(Alert.AlertType.ERROR);
                    failedAlert.setTitle("Failed");
                    failedAlert.setHeaderText(null);
                    failedAlert.setContentText("Issue Operation Failed");
                    failedAlert.showAndWait();
                }
        }else{
            Alert cancelAlert = new Alert(Alert.AlertType.INFORMATION);
            cancelAlert.setTitle("Canceled");
            cancelAlert.setHeaderText(null);
            cancelAlert.setContentText("Issue Operation Canceled");
            cancelAlert.showAndWait();
        }
    }

我试图插入和更新的表是这样的:

 void setupIssueTable(){
       String TABLE_NAME="ISSUE";
       try {
           stmt = conn.createStatement();
           DatabaseMetaData dbm = conn.getMetaData();
           ResultSet tables = dbm.getTables(null, null, TABLE_NAME.toUpperCase(), null);
           if(tables.next()){
               System.out.println("Table "+ TABLE_NAME + "Already exist, ready for go!");
           }else{
               stmt.execute("CREATE TABLE "+ TABLE_NAME + "("
               + "       bookID varchar(200) primary key,\n"
               + "       memberID varchar(200),\n"
               + "       issueTime timestamp default CURRENT_TIMESTAMP,\n"
               + "       renew_Count integer default 0,\n"
               + "       FOREIGN KEY (bookID) REFERENCES BOOK(id),\n"
               + "       FOREIGN KEY (memberID) REFERENCES MEMBER(id)\n"
               + " )");
           }

       } catch (SQLException e) {
           System.err.println(e.getMessage()+".......setupDatabase");
       }finally{
       }
   }

代码有问题吗?我是java新手

标签: javamysqljavafx

解决方案


推荐阅读