I am trying to show my database data into TableView. I have a database named info and there is two columns 'fname' and 'lname' into the table 'data'. I am trying to show the data into tableview format. I've tried the following code-
package imran.jfx.application;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
public class Controller implements Initializable {
@FXML
private TableView<?> dataView;
@FXML
private TableColumn<?, ?> english;
@FXML
private TableColumn<?, ?> bangla;
@FXML
private Button getData;
ResultSet result;
PreparedStatement doQuery;
Connection conn;
String query;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:sqlite::resource:database/info.db";
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
}
@FXML
void showData(ActionEvent event) throws SQLException {
query="select fname,lname from data";
ResultSet result = conn.createStatement().executeQuery(query);
while (result.next())
{
//Can't understand how should I write here to show the data
}
}
}
How should I do it?