Java 如何将SQL变量引入GUI?
我想在GUI中插入文本字段中次要类的变量Java 如何将SQL变量引入GUI?,java,swing,Java,Swing,我想在GUI中插入文本字段中次要类的变量anzahl。计数在minor类中工作,但我不知道现在如何在GUI中获得计数值?在GUI中,我只想查看次要类的值。有人能帮我看一下代码示例吗 小修班: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JLabel; import java.sq
anzahl
。计数在minor类中工作,但我不知道现在如何在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());}
}
}
GUI:(缩写)
要更新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);
}
}
谢谢你的快速帮助!但是textPB1在主类(GUI)中,而不是在次类中。在主GUI类中创建一个方法,并从次类调用该方法,从而实现相同的功能。检查我的更新,你所需要做的就是在你的班级周围传递数据库数据。。。因此它不起作用。我将“textPB1=newjlabel(res.getString(“anzahl”);”放在while语句下的SQL查询中。但是我现在不知道如何在GUI中显示值?我先启动GUI。在第一个窗口中,应该从一开始就显示该值。然后很简单,在加载GUI之前,运行我提供的代码,并在启动之前进行更新…如果没有,请提供完整的GUI代码,以便我可以看到问题在哪里?@FHWS…不确定您是否已检查回该问题,但我的解决方案对你不起作用吗?如果你需要一个不同的想法,请告诉我。
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);
}
}