JDBC_Emp_TableView1
  • TableView<EMP1> visTable = new TableView();
  • Number Data type :: for Integer and Double DataType
  • TableView<EMP1> visTable = new TableView();
    //creating coulmn headers
    TableColumn<EMP1, Number> empnoCol = new TableColumn<>("empno");
Code : JDBC_Emp_TableView1.txt
Code :

// JDBC_Emp_TableView1
//class JavaFXTemplate1
package javafxtemplate1;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
*
* @author Manas9
*/
public class JavaFXTemplate1 extends Application {
//User Interface - display
@Override
public void start(Stage primaryStage) throws Exception {
// Group groot = new Group();
VBox groot = new VBox();
ExternalClass3 ext3 = new ExternalClass3();
//ext3.loaddata();
//
TableView<EMP1> visTable = new TableView();
//creating coulmn headers
TableColumn<EMP1, Number> empnoCol = new TableColumn<>("empno");
TableColumn<EMP1, String> fNameCol = new TableColumn<>("fname");
TableColumn<EMP1, String> jobCol = new TableColumn<>("job");
TableColumn<EMP1, Number> salCol = new TableColumn<>("Sal");
//setting datatype
fNameCol.setCellValueFactory((CellDataFeatures<EMP1, String>
p) -> p.getValue().eNameProperty());
empnoCol.setCellValueFactory((CellDataFeatures<EMP1, Number>
p) -> p.getValue().empIDProperty());
//
jobCol.setCellValueFactory((CellDataFeatures<EMP1, String>
p) -> p.getValue().eJobProperty());
salCol.setCellValueFactory((CellDataFeatures<EMP1, Number>
p) -> p.getValue().esalProperty());
//Display

visTable.getColumns().addAll(empnoCol, fNameCol, jobCol, salCol);
visTable.getItems().addAll(ext3.loaddata());
groot.getChildren().addAll(visTable);
//
Scene scene = new Scene(groot, 350, 150);
primaryStage.setScene(scene);
primaryStage.setTitle("PostgreSQL:: UIControl");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
// Middle Layer
//class ExternalClass3

package javafxtemplate1;

import com.sun.rowset.JdbcRowSetImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;

/**
*
* @author Manas9
*/
public class ExternalClass3 {
//
// Connection and Rowset in one class

public List list1 = new ArrayList();
public List<EMP1> eList = new ArrayList<>();
//
Connection c = null; Statement st = null;
//
public List<EMP1>loaddata() throws SQLException
{

// ResultSet rs = null; // result set object
//String dbURL = "jdbc:mysql://localhost/test?";
String dbURL = "jdbc:postgresql://localhost:5432/pgsdemo1";
// String user = "Manas9"; String pwd = "Manas9237";
String user = "postgres"; String pwd = "postgre_manas9";
//String user = "manas237"; String password = "pwmanas237";
Properties param = new Properties();
List list = new ArrayList();
String sql1 = "Select * from emp";
//
param.put("user",user); param.put("password",pwd);
c = DriverManager.getConnection(dbURL,user,pwd);
st = c.createStatement();
// connecting to db
//ResultSet rs = null;// active connection database result
JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
rowSet.setUrl(dbURL);
rowSet.setUsername(user);
rowSet.setPassword(pwd);
rowSet.setCommand(sql1);
rowSet.execute();
st = c.createStatement();

while (rowSet.next()) {
int eID = rowSet.getInt("empno");
String firstName = rowSet.getString("ename");
String ejob = rowSet.getString("job");
int eDept= rowSet.getInt("deptno");
double esal = rowSet.getDouble("sal");
EMP1 person = new EMP1(eID,firstName, ejob, eDept, esal);
eList.add(person);
/*
list1.add(eID); list1.add(firstName);
list1.add(ejob); list1.add(eDept);
list1.add("-------");
*/
}
return eList;
}

}
:
// Database connection
//class ExternalClass2

package javafxtemplate1;

import java.sql.Connection;
import java.sql.DriverManager;

/**
*
* @author Manas9
*/
public class ExternalClass2 {
Connection cnn= null;
String dbURL = "jdbc:postgresql://localhost:5432/pgsdemo1";
String user = "postgres"; String pwd = "postgre_manas9";
// Creating a function to get a connection
public Connection createConnection() {
System.out.println("Connection Object Created");
// checking connection
if (cnn != null) {
System.out.println("Can't creaate a connection");
return cnn;
} else {
try {
// Getting connection
cnn = DriverManager.getConnection(dbURL,user,pwd);
} catch (Exception e) {
System.out.println(e.toString());
}
}
return cnn;
}

}
 

Expected Output

Runtime Reports: