簡體   English   中英

無法將 javafx 應用程序連接到 mysql 數據庫

[英]Cannot connect javafx Application to mysql database

我正在嘗試將我的 javafx 應用程序連接到 mysql 數據庫。 有一個按鈕叫保存,

哪個,

同樣將 Title、ISBN、Author 和 Publisher 保存到數據庫。

Mysql 連接器版本為 2.0.14

    @FXML
private void savebutton(ActionEvent event) {
    try {
        String url = "jdbc:mysql://localhost:3306/librarymanager";
        String uname="root";
        String pass="";
        String query="INSERT INTO `bookdetails`(`title`, `isbn`, `author`, `publisher`, `isavailable`) VALUES ("+title.getText()+","+isbn.getText()+","+author.getText()+","+publisher.getText()+","+Boolean.TRUE+")";
        Class.forName("org.gjt.mm.mysql.Driver");
        Connection con=DriverManager.getConnection(url, uname, pass);
        Statement st=con.createStatement();
        st.executeUpdate(query);
        st.close();
        con.close();
    } catch (ClassNotFoundException |SQLException ex) {
       System.out.println(ex);
    }
}

這是按下保存按鈕時調用的 function。

全 controller class 是...

公共 class FXMLDocumentController 實現 Initializable {

private Label label;
@FXML
private JFXButton save;
@FXML
private JFXButton cancel;
@FXML
private JFXTextField title;
@FXML
private JFXTextField isbn;
@FXML
private JFXTextField author;
@FXML
private JFXTextField publisher;



@Override
public void initialize(URL url, ResourceBundle rb) {
   
    RequiredFieldValidator fortitle=new RequiredFieldValidator();
    RequiredFieldValidator forisbn=new RequiredFieldValidator();
    title.getValidators().add(fortitle);
    title.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
        if(!newValue){
            fortitle.setMessage("Enter the title");
            title.validate();
        }
    });
    isbn.getValidators().add(forisbn);
    isbn.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
        if(!newValue){
            forisbn.setMessage("Enter ISBN");
            isbn.validate();
        }
    });
}    

@FXML
private void savebutton(ActionEvent event) {
    try {
        String url = "jdbc:mysql://localhost:3306/librarymanager";
        String uname="root";
        String pass="";
        String query="INSERT INTO `bookdetails`(`title`, `isbn`, `author`, `publisher`, `isavailable`) VALUES ("+title.getText()+","+isbn.getText()+","+author.getText()+","+publisher.getText()+","+Boolean.TRUE+")";
        Class.forName("org.gjt.mm.mysql.Driver");
        Connection con=DriverManager.getConnection(url, uname, pass);
        Statement st=con.createStatement();
        st.executeUpdate(query);
        st.close();
        con.close();
    } catch (ClassNotFoundException |SQLException ex) {
       System.out.println(ex);
    }
}

@FXML
private void cancelbutton(ActionEvent event) {
       Button btn=(Button)event.getSource();
       Stage stage =(Stage)btn.getScene().getWindow();
       stage.close();
}

Netbeans IDE 出錯 -無法連接到 localhost:3306 上的 MySQL 服務器。 您嘗試連接的機器/端口上是否有運行 MySQL 服務器? (java.lang.NumberFormatException)。我知道這個錯誤之前已經被問過,但我沒有像那個代碼那樣犯過錯誤。 我從 xaamp 啟動服務器,它啟動時沒有任何問題。 我可以在瀏覽器中打開 localhost:8080/phpmyadmin/。

我無法指出可能的問題,請您幫助我。

只是查詢錯誤。

代替

String query="INSERT INTO `bookdetails`(`title`, `isbn`, `author`, `publisher`, `isavailable`) VALUES ("+title.getText()+","+isbn.getText()+","+author.getText()+","+publisher.getText()+","+Boolean.TRUE+")";

我用了

 String query="insert into bookdetails values (?, ?, ?, ?, ?)";
 PreparedStatement st=con.prepareStatement(query);
                st.setString(1, title.getText());
                st.setString(2, isbn.getText());
                st.setString(3, author.getText());
                st.setString(4, publisher.getText());
                st.setBoolean(5, true);
                st.executeUpdate();
                st.close();

無論如何,它可能會幫助像我這樣的人。

暫無
暫無

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

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