Saturday, 17 October 2015

How To Load And Refresh Data Base Data In Table JavaFx

In this tutorial we are going to discussed how to load database data in table after the table have loaded sometimes data becomes change we should have a option to refresh our data in this program we have show you how to refresh table in javafx on a click.This enable user to continuously update database data.  

CODING:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
public class mybej extends Application 
{

private final TableView<myData> table = new TableView<>();
private final ObservableList<myData> data =
FXCollections.observableArrayList();
String s,s1,s2,s3,s4,s5;
Button b1 = new Button("REFRESH");
ResultSet database,database1;
Statement st,st1;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TableColumn firstCol = new TableColumn("First Name");
firstCol.setMinWidth(100);
firstCol.setCellValueFactory(
new PropertyValueFactory<>("name"));
TableColumn secCol = new TableColumn("Fax-Number");
secCol.setCellValueFactory(
new PropertyValueFactory<>("number"));
TableColumn lastCol = new TableColumn("Email");
lastCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
table.setItems(data);
table.getColumns().addAll(firstCol, secCol,lastCol);
table.setMinWidth(230);
table.setLayoutX(10);
table.setLayoutY(40);
b1.setLayoutX(5);
b1.setLayoutY(5);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st1=cn.createStatement();
database=st1.executeQuery("select * from load");
while(database.next())
{
s = database.getString(1);
s1 = database.getString(2);
s2 = database.getString(3);
data.add(new myData(s,s1,s2));
}  
st.close();
database.close();
cn.close();
}
catch(Exception e)
{
System.out.print(e);

}
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override 
public void handle(ActionEvent e) {
try
{
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st=cn.createStatement();

database=st1.executeQuery("select * from load");
data.removeAll(data);
while(database.next())
{
s3 = database.getString(1);
s4 = database.getString(2);
s5 = database.getString(3);

data.add(new myData(s3,s4,s5));

}
st.close();
cn.close();
}   catch(Exception ex)
{
System.out.print(ex);
}
}
});
Group root = new Group();
root.getChildren().addAll(table,b1);
stage.setTitle("Table with Database");    
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.show();
}

public static class myData {

private final SimpleStringProperty Name;
private final SimpleStringProperty email;
private final SimpleStringProperty number;
private myData(String fName,String fnum,String email) {
this.Name = new SimpleStringProperty(fName);
this.email = new SimpleStringProperty(email);
this.number = new SimpleStringProperty(fnum);
}


public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
public String getName() {
return Name.get();
}
public void setName(String fName) {
Name.set(fName);
}
public String getNumber() {
return number.get();
}
public void setNumber(String fName) {
number.set(fName);
}

}

OUTPUT:





3 comments:

  1. Thanks it's work for me. keep it up

    ReplyDelete
  2. Thanks very muuuuuucccch!

    ReplyDelete
  3. Cool and I have a dandy offer you: House Renovation Designer Near Me house reno shows

    ReplyDelete