Javafx fetching data from database to a table.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class ValuesFromDatabaseToTable extends Application
{
PreparedStatement ps;
Connection conn;
ResultSet rs;
String usernameSession;
private final TableView<Contacts> table = new TableView<>();
private final ObservableList<Contacts>data = FXCollections.observableArrayList();
public static void main(String[]args)
{
Application.launch(args);
}
@Override
public void start(Stage primaryStage)
{
BorderPane tablebp = new BorderPane();
Scene scene = new Scene(tablebp,1200,500);
//creating the tablecolumn
TableColumn fnameC = new TableColumn("FirstName");//column for firstname
fnameC.setMinWidth(200);
fnameC.setCellValueFactory(new PropertyValueFactory<>("FirstName"));
TableColumn lnameC = new TableColumn("LastName");//column for lastname
lnameC.setMinWidth(200);
lnameC.setCellValueFactory(new PropertyValueFactory<>("LastName"));
TableColumn surnameC = new TableColumn("SurName");//column for surname
surnameC.setMinWidth(200);
surnameC.setCellValueFactory(new PropertyValueFactory<>("SurName"));
TableColumn phonenumberC = new TableColumn("PhoneNumber");//column for phonenumber
phonenumberC .setMinWidth(200);
phonenumberC .setCellValueFactory(new PropertyValueFactory<>("PhoneNumber"));
TableColumn emailC = new TableColumn("Email");//column for surname
emailC.setMinWidth(200);
emailC.setCellValueFactory(new PropertyValueFactory<>("Email"));
table.getColumns().addAll(fnameC,lnameC,surnameC,phonenumberC,emailC);
table.setItems(data);
tablebp.setCenter(table);//setting table on the center of the table
//creating button for querying data from database to table
Button loadB = new Button("load");
tablebp.setPadding(new Insets(10,0,10,0));
tablebp.setBottom(loadB);//setting the button at the bottom of the borderpane
//setting action to query data from table
loadB.setOnAction((ActionEvent t) -> {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/address","root","kevo");//my database connection string my database name is address username is root and password is kevo
PreparedStatement ps = conn.prepareStatement("SELECT * FROM contacts");
rs = ps.executeQuery();
while(rs.next())
{
data.add(new Contacts(rs.getString("fname"),rs.getString("lname"),rs.getString("surname"),rs.getString("pnumber"),rs.getString("email")));
}
conn.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(ValuesFromDatabaseToTable.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(ValuesFromDatabaseToTable.class.getName()).log(Level.SEVERE, null, ex);
}
});
primaryStage.setTitle("Database Values To Table");
primaryStage.setScene(scene);
primaryStage.show();
}
public static class Contacts
{
private final SimpleStringProperty FirstName;
private final SimpleStringProperty LastName;
private final SimpleStringProperty SurName;
private final SimpleStringProperty PhoneNumber;
private final SimpleStringProperty Email;
private Contacts(String fname,String lname,String surname,String pnumber,String email)
{
this.FirstName = new SimpleStringProperty(fname);
this.LastName = new SimpleStringProperty(lname);
this.SurName = new SimpleStringProperty(surname);
this.PhoneNumber = new SimpleStringProperty(pnumber);
this.Email = new SimpleStringProperty(email);
}
public String getFirstName()
{
return FirstName.get();
}
public void setFirstName(String fname)
{
FirstName.set(fname);
}
public String getLastName()
{
return LastName.get();
}
public void setLastName(String lname)
{
LastName.set(lname);
}
public String getSurName()
{
return SurName.get();
}
public void setSurName(String surname)
{
SurName.set(surname);
}
public String getPhoneNumber()
{
return PhoneNumber.get();
}
public void setPhoneNumber(String pnumber)
{
PhoneNumber.set(pnumber);
}
public String getEmail()
{
return Email.get();
}
public void setEmail(String email)
{
Email.set(email);
}
}
}
The code above produces the following result:
Comments
Post a Comment