title: How to fill up a TableView with database data
body: <p>I've been trying to load a <code>TableView</code> with data queried from a database, but can't seem to get it to work.</p>
  <p>This is my first attempt at trying to populate a <code>TableView</code> with database query items – in case my code seems mungled and far from good.</p>
  <p>The FXML was done via JavaFX SceneBuilder.</p>
  <p>This is the database query class:</p>
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.fxml.FXML;
  import javafx.scene.control.TableView;
  
  public class StudentInfo {
      static String JDBC_DRIVER = &quot;org.h2.Driver&quot;;
      static String DB_URL = &quot;jdbc:h2:file:C:/WAKILI/WAKILIdb&quot;;
      //  Database credentials
      static final String USER = &quot;sa&quot;;
      static final String PASS = &quot;&quot;;
      
      public static Connection conn = null;
      @FXML
      private TableView&lt;StudentInfo&gt; lovelyStudents;
      
      private ObservableList data;
  
      // Public static ObservableList&lt;COA&gt; getAllCOA(){
      public void getAllstudentInfo() {
          Statement st = null;
          ResultSet rs;
          String driver = &quot;org.h2.Driver&quot;;
  
          try {
              Class.forName(driver);
              conn = DriverManager.getConnection(DB_URL, USER, PASS);
              st = conn.createStatement();
              String recordQuery = (&quot;SELECT id, KIWI FROM KIWI&quot;);
              
              rs = st.executeQuery(recordQuery);
              while (rs.next()) {
                  ObservableList row = FXCollections.observableArrayList();
                  
                  for (int i = 1; i &lt;= rs.getMetaData().getColumnCount(); i++) {
                      row.add(rs.getString(i));
                      System.out.println(row);
                  }
                  
                  data.add(row);
                  
              }
              lovelyStudents.setItems(data);
              
          } catch (ClassNotFoundException | SQLException ex) {
              // CATCH SOMETHING
          }
      }
  }
  </code></pre>
  <p>This is the FXML script generated via JavaFX scene builder:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  
  &lt;AnchorPane id=&quot;AnchorPane&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;700.0&quot; xmlns:fx=&quot;http://javafx.com/fxml&quot; fx:controller=&quot;wakiliproject.SampleController&quot;&gt;
    &lt;children&gt;
      &lt;TableView prefHeight=&quot;400.0&quot; prefWidth=&quot;700.0&quot; AnchorPane.bottomAnchor=&quot;0.0&quot; AnchorPane.leftAnchor=&quot;0.0&quot; AnchorPane.rightAnchor=&quot;0.0&quot; AnchorPane.topAnchor=&quot;0.0&quot;&gt;
        &lt;columns&gt;
          &lt;TableColumn prefWidth=&quot;75.0&quot; text=&quot;Column X&quot; /&gt;
        &lt;/columns&gt;
      &lt;/TableView&gt;
    &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>