英文:
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) {
// ...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论