java - 如何将 SQL 变量带入 GUI?
问题描述
我想将anzahl
来自次要类的变量插入到我的 GUI 中的文本字段中。计数在次要类中起作用,但我不知道现在如何在 GUI 中获取计数值?在 GUI 中,我只想查看次要类的值。有人可以帮我提供代码示例吗?
小班:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JLabel;
import java.sql.DriverManager;
public class Count {
static Connection conn;
static Statement eintrag;
static Statement eintrag2;
static JLabel textPB1;
static String ausgabe;
public static String anzahl;
Statement abfrage;
int i;
ResultSet res;
private int num;
Count() {
try {
Class.forName("org.mariadb.jdbc.Driver");
Connection con = java.sql.DriverManager.getConnection("jdbc:mariadb://fm-s012mp.fhws.de","Java","xyc");
Statement s = con.createStatement();
ResultSet res;
res = s.executeQuery("SELECT COUNT (*) AS anzahl FROM `lagersystem_test`.`00_hauptdatenbank` WHERE Boxinhalt > '0'" );
while (res.next() ) {
System.out.print(res.getString("anzahl") );
GUI_Lager.setTextExternally(res.getString("anzahl"));
}
res.close();
s.close();
con.close();
}
catch (Exception e) { System.out.println(""+e.getMessage());}
}
}
图形用户界面:(简称)
public class GUI_Lager extends JFrame {
private static final long serialVersionUID = 1L;
JLabel textPB; // "Text Prozessbar
static JLabel textPB1;
(....)
public GUI_Lager() {
textPB1 = new JLabel(""); // Variable from the Class
textPB1.setBounds(200, 10, 400, 25);
textPB1.setVisible(true);
add(textPB1);
(....)
}
public static void setTextExternally(String text) {
textPB1.setText(text);
// TODO Auto-generated method stub
}
}
}
解决方案
要更新 JLabel,您将使用:
yourLabel.setText("your text");
因此,在您问题中提供的代码的上下文中(假设它可以正常工作),您可以这样做:
while (res.next() == true) {
//System.out.print(res.getString("anzahl") );
//This "anzahl" i want to have in my GUI
textPB1.setText(res.getString("anzahl")); //Now you should have the label set
}
如果由于某种原因 JLabel 不喜欢在循环中以这种方式进行更改,您也可以像这样实例化一个新的对象引用:
textPB1 = new JLabel(res.getString("anzahl"));
更新1:
如果您需要从不同的类设置值,只需使用 textPB1 在类中创建一个方法,您将从从中获取 DB 值的类中调用该方法,如下所示:
public static void setTextExternally(String text){
textPB1.setText(text);
//or
textPB1 = new JLabel(text);
}
然后在之前的循环中,执行以下操作:
while (res.next() == true) {
//label should be set in other class using setter method below
MainGUI.setTextExternally(res.getString("anzahl"));
}
更新 2:
此更新显示了与我已经提供的方法一起使用的 Swing 应用程序的特定示例,现在我看到了您的基本 GUI 代码,并进行了一些更改。如果您需要一个直接的示例来处理,我建议您从这些文件中构建:
你的 GUI_Lager 类:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GUI_Lager extends JFrame {
private static final long serialVersionUID = 1L;
JLabel textPB1;
public GUI_Lager() {
//no need for constructor, so it can be null
}
public void showGUI() {
//let's make the GUI here
this.setSize(300, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
//-----------------your code
//let's give it dummy text at first to ensure we can change it
textPB1 = new JLabel("Dummy Text");
textPB1.setBounds(200, 10, 400, 25);
textPB1.setVisible(true);
add(textPB1);
//-----------------your code
}
public void setTextExternally(String text) {
//alters the text of class variable/label textPB1
textPB1.setText(text);
}
}
然后是 Count 类:
import java.util.Scanner;
public class Count {
public static void main(String[] args) {
GUI_Lager gui = new GUI_Lager();
gui.showGUI(); //you must show the GUI first
//now we change the value, it will be done using your SQL selection from before
gui.setTextExternally("Awesome, it works!");
//launch Count.java and the Swing application will have a
// single label that says "Awesome, it works!"
//...change your data as needed based on your specific implementation
//but let's also show how to change it using console input
Scanner scan = new Scanner(System.in);
System.out.println("What do you want to change the text label to?");
String text = scan.nextLine().trim();
gui.setTextExternally(text);
}
}
推荐阅读
- c# - 将布尔数组转换为计数器
- c# - 如何避免消息进入“deadletter”队列 azure 服务总线
- python - 为什么python在opencv中为对象检测抛出断言错误?
- javascript - Google Analytics Gtag JS:设置自定义维度的代码不起作用?
- matlab - 如何编写Matlab程序来确定正方形中的点
- php - 如何在没有枢轴模型的情况下在 Laravel 中存储多对多关系?
- java - 尝试执行脚本时出现错误“net.serenitybdd.core.exceptions.SerenityManagedException: No session ID”
- amazon-web-services - AWS Sagemaker 自定义小部件安装 Jupyter Notebook 或 Lab
- typescript - Typescript 如何使用非静态键编写嵌套对象类型
- python - 如何使用 DBFS 中的库而不是 Databricks 中的预安装库