java - 如何在 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”按钮后)?
有人能帮我吗?先感谢您。
解决方案
推荐阅读
- php - 将值传递给另一个类 php
- java - 需要在一个 git 存储库中调用两个分支
- amazon-web-services - 每次我在 Cloudflare 后面运行代码时都会出现 520 错误
- ms-word - 如何在文档正文中使用 Office.js 插入表格时添加合并字段
- post - 如何放心地使用 pojo 和 lombok builder 构建请求主体
- typeorm - 是否可以使用 Typeorm 进行完全连接?
- json - 在 Mule 中使用 Dataweave 读取数组值
- html - 为什么 ul 元素的填充区域和 CSS 中的子元素分开?
- java - HttpErrorResponse : 内部服务器错误
- r - 如何从因子变量中按水平每天从大型数据集中提取并估计其他统计数据