我正在尝试将javafx应用程序连接到mysql数据库。有一个名为保存的按钮,
其中
同样将标题,ISBN,作者和发布者保存到数据库中。
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实现可初始化的{
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服务器。 (java.lang.NumberFormatException)。我知道此错误已在以前询问过,但我没有做过类似该代码的错误。 我从xaamp启动服务器,启动时没有任何问题。 我可以在浏览器中打开localhost:8080 / phpmyadmin/。
我无法指出可能的问题,请您帮我。
答案 0 :(得分:0)
只是查询错误。
代替
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();
无论如何,它可能会帮助像我这样的人。