首页 > 解决方案 > 如何在 JavaFX 的文本中显示数据库中的数据(列),以便在单击按钮后从列中获取下一行?

问题描述

我是这里的新手。首先,我想为我糟糕的英语道歉。这不是我的母语。我已经很久没有使用这种语言了。

我的代码有问题。我想在文本中显示我的数据库列的第一行,当我单击一个按钮时,我希望得到我的数据库列的第二行。所以它应该一直持续到我没有更多的行可以显示在那个特定的列中。

到目前为止,我已经设法在“文本”中显示数据库中的一行,但该行是数据库中的最后一行:)

这是 Database 类中的方法:

    public static String deutscheVokAn() {
    Connection conn = null;
    Statement stmt = null;
    String b = null;
    try {
        conn = DriverManager.getConnection(connString);
        
        String str= "SELECT " + VocabelDeutsch + " FROM " + VocabelTable;
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(str);
        while (rs.next()) {
        b = rs.getString(VocabelDeutsch);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return b;
}

这就是 JavaFx 中的场景和应该显示的文本“txt”:

Button zurueck = new Button("Zurück zum Hauptmenü");
    Label evl = new Label("Deutsch");
    Text txt = new Text();
    richtigfalsch = new Label();
    txtrichtig = new TextField("");
    txtrichtig.setPrefColumnCount(40);
    Button weiter = new Button("Weiter");
    
    HBox h1 = new HBox(10, txtrichtig, weiter);
    VBox v1 = new VBox(10, zurueck, evl, txt, richtigfalsch, h1);
    v1.setPadding(new Insets(5));
    Scene scene2 = new Scene(v1, 550, 300);

这是单击“weiter”(英文:Next)按钮后的实现:

weiter.setOnAction(e -> {
        primaryStage.show();
        txt.setText(Datenbank.deutscheVokAn());
        checkAnswer();
    
    });

我不知道是否有人也想查看 checkAnswer () 方法,但它看起来是这样的:

    public void checkAnswer() {
    
        if (txtrichtig.getText().equals(Datenbank.englischeVokAn())) {
            richtigfalsch.setText("Richtig");
        } else {
            richtigfalsch.setText("Falsch");
        }
    
}

如果有人想在这里查看方法 englishVokAn():

    public static String englischeVokAn() {
    Connection conn = null;
    Statement stmt = null;
    String b = null;
    try {
        conn = DriverManager.getConnection(connString);
        
        String str= "SELECT " + VocabelEnglisch + " FROM " + VocabelTable;          
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(str);
        while (rs.next()) {
        b = rs.getString(VocabelEnglisch);
        }
   
        
    } catch (Exception e) {
        e.printStackTrace();
    }
    return b;
}

我的数据库类:

public class Datenbank {

private static final String DBlocation = "C:\\Users\\musta\\Vokabeltrainer15_db";               
private static final String connString = "jdbc:derby:" + DBlocation + ";create=true";   
private static final String VocabelTable = "Vokabel";
private static final String VocabelID = "VokabelID";
private static final String VocabelDeutsch = "VokabelDeutsch";
private static final String VocabelEnglisch = "VokabelEnglisch";

实际的问题是:当我单击“下一步”按钮时,我不应该看到该列的第二行吗?(显然不是:D)如何更改我的代码,以便在文本“txt”中的“VocabelEnglisch”列中看到数据(第一行,然后单击第二行“weiter”按钮后)?

有人能帮我吗?先感谢您。

标签: javasqljavafxderby

解决方案


推荐阅读