我正在尝试将我的数据库数据显示到TableView中。我有一个名为info的数据库,表'数据'中有两列“ fname ”和“ lname ”。我试图将数据显示为tableview格式。我尝试过以下代码 -
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
}
}
}
我该怎么做?