TomaszBag
TomaszBag

Reputation: 1

JavaFX Scene builder display variable on start program

Hello I try to do something like getData from MySQL on start method in JavaFX

Now in Intellji I click "Run program" and then window is appear on the screen.

I have one Button "Get data" and after I clicked on it I get data from Mysql to show it in Label.

It is some solution that when we click "Run program" and JavaFX show window of our program and this Data from MySQL will be automatically display in label?

I mean how to run method getData(get data from MySQL) in public void start?

I display variable "Label labelek;" after click Button "Label labelek;" is in getData method

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);
        }
    }
 }

Upvotes: 0

Views: 1694

Answers (2)

TomaszBag
TomaszBag

Reputation: 1

After I click Button with onAction (SceneBuilder) getData I can only see in IDEA "Records from DataBase" without TemperaturaPow :/

Controller Class in Scene Builder is : sample.DBConnect

It is really so difficult ;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;
}

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);
}

PrintScreen

Upvotes: 0

MBec
MBec

Reputation: 2210

Remove Label labelek from DBConnect class. Create separate SampleController class, in initalize() method invoke DBConnect connect = new DBConnect() and attach your data to 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);
    }
}

Upvotes: 1

Related Questions