如何将JavaFX FXML复选框连接到MySQL数据库?

huangapple go评论71阅读模式
英文:

How do i connect a JavaFX FXML checkbox to a MySQL database?

问题

我是初学者,想在JavaFX应用中制作复选框,有人可以告诉我如何将这个复选框方法与我的数据库连接起来吗:

@FXML
private void handleOptions(ActionEvent e){
    String message = "";
    if(checkFc.isSelected()){
        message += checkFc.getText();
        return;
    }
}

我想将复选框的值应用于这个方法:

public void addUsers (){    
    conn = mysqlconnect.ConnectDb();
    String sql = "insert into users (username,password,email,fc)values(?,?,?,? )";
    try {
        pst = conn.prepareStatement(sql);
        pst.setString(1, txtUsername.getText());
        pst.setString(2, txtPassword.getText());
        pst.setString(3, txtEmail.getText());
        pst.setString(4, checkFc.getText());

尽管getText方法能够工作,但它会无论复选框是否被选中,都会获取该值。当调用handleOptions方法时,我会得到"cannot resolve method"的错误,这是有道理的,因为这些是预处理语句(prepared statements)。所以,有没有办法让我的方法成为一个预处理语句,或者也许我可以以完全不同的方式连接我的方法?拜托了,我真的需要在这个问题上得到帮助。

英文:

I am a beginner trying to make checkboxes in a JavaFX app, can someone please tell me how do I connect to my database this checkbox method :

    @FXML
    private void handleOptions(ActionEvent e){
        String message= "";
        if(checkFc.isSelected()){
            message += checkFc.getText();
            return;
        }

i want to apply the value of the checkbox into this method :

public void addUsers (){    
    conn = mysqlconnect.ConnectDb();
    String sql = "insert into users (username,password,email,fc)values(?,?,?,? )";
    try {
        pst = conn.prepareStatement(sql);
        pst.setString(1, txtUsername.getText());
        pst.setString(2, txtPassword.getText());
        pst.setString(3, txtEmail.getText());
        pst.setString(4, checkFc.getText());

the getText method, though it works, it gets the value regardless if the checkbox is checked or not, while calling for the handleOptions method, I get cannot resolve method, which makes sense since these are prepared statements, so is there a way to make my method a preparedStatement or maybe I can connect my method another way altogether, please I really need help on this one.

答案1

得分: 1

这里的问题并不是非常清楚。为什么不简单地像下面这样做呢?

public void addUsers (){    
    conn = mysqlconnect.ConnectDb();
    String sql = "insert into users (username,password,email,fc)values(?,?,?,? )";
    try {
        pst = conn.prepareStatement(sql);
        pst.setString(1, txtUsername.getText());
        pst.setString(2, txtPassword.getText());
        pst.setString(3, txtEmail.getText());
        if (checkFc.isSelected()) {
            pst.setString(4, checkFc.getText());
        } else {
            pst.setString(4, null); 
            // 或者根据您的需求,将 null 替换为 ""(空字符串)
        }
        // ...
    } catch (SQLException exc) {
        // ...
    }
}
英文:

It's not really clear what the question is here. Why don't you just do something like

public void addUsers (){    
    conn = mysqlconnect.ConnectDb();
    String sql = "insert into users (username,password,email,fc)values(?,?,?,? )";
    try {
        pst = conn.prepareStatement(sql);
        pst.setString(1, txtUsername.getText());
        pst.setString(2, txtPassword.getText());
        pst.setString(3, txtEmail.getText());
        if (checkFc.isSelected()) {
            pst.setString(4, checkFc.getText());
        } else {
            pst.setString(4, null); 
            // or "" instead of null, depending on what you need
        }
        // ...
    } catch (SQLException exc) {
        // ...
    }
}

huangapple
  • 本文由 发表于 2020年10月7日 22:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64246171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定