【问题标题】:Cannot connect javafx Application to mysql database无法将 javafx 应用程序连接到 mysql 数据库
【发布时间】:2020-11-26 06:13:58
【问题描述】:

我正在尝试将我的 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);
    }
}

这是按下保存按钮时调用的函数。

完整的控制器类是...

公共类 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/。

我无法指出可能的问题,请您帮帮我。

【问题讨论】:

    标签: java mysql phpmyadmin


    【解决方案1】:

    只是查询错误。

    而不是

    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();
    

    无论如何,它可能会帮助像我这样的人。

    【讨论】:

      猜你喜欢
      • 2012-10-09
      • 2020-04-23
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多