gpt4 book ai didi

java - 用于插入到 sql : event handler is not in the Namespace or there is an error in the script 的按钮

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:58 25 4
gpt4 key购买 nike

我正在使用 javafx 和 sqllite 构建这个程序我在 fxml 中有带有 fx:id button1 的按钮和 onAction: WriteToSql

package valgykla;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;

/**
* FXML Controller class
*
* @author Lukas
*/
public class MeniuController implements Initializable {
@FXML
private Button button1;

@FXML
public static void WriteToSql(ActionEvent sql){
Connection con = null;
PreparedStatement prSt = null;
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.
getConnection("jdbc:sqlite:database.db");
String query = "insert into Meniu(name,code) values(?,?)";
prSt = con.prepareStatement(query);
prSt.setString(1, "jack");
prSt.setString(12, "02545");
int count = prSt.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block

}
// TODO Auto-generated catch block
finally{
try{
if(prSt != null) prSt.close();
if(con != null) con.close();
} catch(Exception ex){}


}
}

public void initialize(URL url, ResourceBundle rb) {
// TODO
}


}

不幸的是我得到了错误

Caused by: javafx.fxml.LoadException: Error resolving onAction='#WriteToSql', either the event handler is not in the Namespace or there is an error in the script.
file:/C:/Users/Lukas/Desktop/lukasX/Valgykla/dist/run1382723305/Valgykla.jar!/valgykla/Meniu.fxml:29

最佳答案

WriteToSql 方法是静态。 JavaFX 不再考虑事件处理程序的 Controller 的静态方法...(另请参阅 javafx 8 compatibility issues - FXML static fields )

只需删除 static 关键字即可解决该问题。此外,由于您使用 @FXML 注释了该方法,您还可以将其设为 private:

@FXML
private void WriteToSql(ActionEvent sql){
...

关于java - 用于插入到 sql : event handler is not in the Namespace or there is an error in the script 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com