您好我尝试在JavaFX
中的start方法上执行类似MySQL的getData现在在Intellji中,我点击"运行程序"然后窗口出现在窗口上。
我有一个按钮"获取数据"在我点击之后,我从Mysql获取数据以在Label中显示它。
当我们点击"运行程序"这是一些解决方案。和JavaFX显示我们程序的窗口,这个来自MySQL的数据会自动显示在标签中吗?
我的意思是如何在public void start中运行方法getData(从MySQL获取数据)?
我显示变量"标签labelek;"单击按钮后 "标签labelek;"在getData方法
中public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 600, 275));
DBConnect connect = new DBConnect();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
DBCONNECT
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import java.sql.*;
public class DBConnect {
private Connection con;
private Statement st;
private ResultSet rs;
@FXML
public Label labelek;
//utworzenie konstruktora
public DBConnect(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://xxx","xxx",
"xxx");
st = con.createStatement();
} catch (Exception ex){
System.out.println("Error: " + ex);
}
}
public void getData(){
try {
String query = "select * from x order by id desc limit 1";
rs = st.executeQuery(query);
System.out.println("Records from DataBase");
while(rs.next()){
String TemperaturaPow = rs.getString("TemperaturaPow");
labelek.setText("Temperature is " + temperathure);
}
} catch (Exception ex){
System.out.println("Error: " + ex);
}
}
}
答案 0 :(得分:1)
从Label labelek
课程中删除DBConnect
。在SampleController
方法调用initalize()
中创建单独的DBConnect connect = new DBConnect()
课程,并将您的数据附加到Label labelek
。
<强> SampleController 强>
public class SampleController implements Initializable {
@FXML
private Label labelek;
@FXML
private Button getDataButton;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
DBConnect connect = new DBConnect();
String data = connect.getData();
labelek.setText(data);
}
}
答案 1 :(得分:0)
单击带有onAction(SceneBuilder)getData的Button后,我只能在IDEA中看到 &#34;来自DataBase的记录&#34;没有TemperaturaPow:/
Scene Builder中的控制器类是:sample.DBConnect
真的很难; P?
public String getData(){
try {
String query = "select * from danet order by id desc limit 1";
rs = st.executeQuery(query);
System.out.println("Records from DataBase");
while(rs.next()){
String DataCzas = rs.getString("dataczas");
String TemperaturaPow = rs.getString("TemperaturaPow");
String WilgotnoscPow = rs.getString("WilgotnoscPow");
String Cisnienie = rs.getString("baro");
String dewPoint = rs.getString("dewPoint");
String heatIndexC = rs.getString("heatIndexc");
String comment = rs.getString("comment");
return TemperaturaPow;
}
} catch (Exception ex){
System.out.println("Error: " + ex);
}
return null;
}
公共类SampleController实现Initializable {
@FXML
private Label labelek;
@FXML
private Button getDataButton;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
DBConnect connect = new DBConnect();
String data = connect.getData();
labelek.setText(data);
}