I'm still a noob in JavaFX, I can print the content of the table that I query, the problem is that I want the content to be placed inside the tableview. This is the code that I have done.

public class TableViewController  implements Initializable{


@FXML
private TableView<studentInfo> tblViewer = new TableView();
@FXML
private TableColumn colID = new TableColumn();
@FXML
private TableColumn colName = new TableColumn();
@FXML
private TableColumn colAddress = new TableColumn();
@FXML
private TableColumn colAge = new TableColumn();
@FXML
private TableColumn colContact = new TableColumn();
@FXML
private Button cmdTest;

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
    System.out.println("READ");
    System.out.println(this.getClass().getSimpleName() + ".initialize"); 
    cmdTest.setOnAction(new EventHandler<ActionEvent>() {            
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Button Pressed");

                colID.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("id"));
                colName.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("name"));
                colAddress.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("address"));
                colAge.setCellValueFactory(new PropertyValueFactory<studentInfo, Integer>("age"));
                colContact.setCellValueFactory(new PropertyValueFactory<studentInfo, String>("contact_num"));
                tblViewer.getItems().setAll(getAllstudentInfo());
            }
        } );       
}

public class studentInfo{

    private final SimpleIntegerProperty idCol;
    private final SimpleStringProperty nameCol;
    private final SimpleStringProperty addressCol;
    private final SimpleIntegerProperty ageCol;
    private final SimpleStringProperty contact_numCol;

    public studentInfo(Integer id, String name, String address, Integer age, String contact_num){
        this.idCol = new SimpleIntegerProperty(id);
        this.nameCol = new SimpleStringProperty(name);
        this.addressCol = new SimpleStringProperty(address);
        this.ageCol = new SimpleIntegerProperty(age);
        this.contact_numCol = new SimpleStringProperty(contact_num);
      }  
      public Integer getidCol(){
          return idCol.get();
      }
      public void setidCol(Integer id){
          idCol.set(id);      
      }
      public String getnameCol(){
          return nameCol.get();
      }
      public void setnameCol(String name){
          nameCol.set(name);
      }
      public String getaddressCol(){
          return addressCol.get();
      }
      public void setaddressCol(String address){
          addressCol.set(address);
      }
      public Integer getageCol(){
          return ageCol.get();
      }
      public void setageCol(Integer age){
          ageCol.set(age);
      }
      public String getcontact_numCol(){
          return contact_numCol.get();
      }
      public void setcontact_numCol(String contact_num){
          contact_numCol.set(contact_num);
      }        
}

//public static ObservableList<COA> getAllCOA(){
public List<studentInfo> getAllstudentInfo(){
    Connection conn;
    List ll = new LinkedList();
    Statement st;
    ResultSet rs;
    String url = "jdbc:mysql://localhost/java_test";
    String user = "root";
    String pass = "";
    String driver = "com.mysql.jdbc.Driver";

    try{
        Class.forName(driver);
        conn = DriverManager.getConnection(url, user, pass);
        st = conn.createStatement();
        String recordQuery = ("Select * from student");            
        rs = st.executeQuery(recordQuery);
        while(rs.next()){                
            Integer id = rs.getInt("id");
            String name = rs.getString("name");
            String address = rs.getString("address");
            Integer age = rs.getInt("age");
            String contact_num = rs.getString("contact_num");

            ll.add(new studentInfo(id, name, address, age, contact_num));
            System.out.println(id +","+ name +","+ address +","+ age +","+ contact_num +" "+"added.");
        }            
    }catch(ClassNotFoundException | SQLException ex){
        Logger.getLogger(TableViewController.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ll;        
}

Please help me.

Recommended Answers

All 3 Replies

and what does this have to do with Java?

ahm.its for javafx. I wanted to populate a table.

i am getting error in rs = st.executeQuery(recordQuery);
please fix this thanks for solve in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.