如何将信息添加到 sqlite 数据库,然后使用 javafx 显示信息?
Posted
技术标签:
【中文标题】如何将信息添加到 sqlite 数据库,然后使用 javafx 显示信息?【英文标题】:How to add information to a sqlite database and then display the information using javafx? 【发布时间】:2015-04-22 15:21:51 【问题描述】:public static void addToQueueTable(Patient p)
try
// open connection to database
conn = ConnectionFactory.getConnection();
// create statement
stmt = conn.createStatement();
String sql = "insert into queueTime values('" + p.getNhsNumber()
+ "', CURRENT_TIMESTAMP)";
// execute update
stmt.executeUpdate(sql);
catch (Exception e)
e.printStackTrace();
System.out.println("Error on Building Data");
finally
// close all open resources in the database
DbUtil.close(stmt);
DbUtil.close(conn);
以上是我将详细信息(NhsNumber 和当前时间戳)添加到我的数据库的代码 - 只是想知道如何使用 javafx 显示它? 患者分类如下:
public Patient(String nhsNumber, String firstName,
String lastName, String postCode)
super(nhsNumber,firstName, lastName
, postCode);
使用 javafx 我可以显示某些详细信息(见下文),但我不确定如何也可以从数据库中获取时间戳?任何帮助将不胜感激!
public class QueueTabPageController implements Initializable
@FXML
private TableView<Patient> tableView;
@FXML
private TableColumn<Patient, String> firstNameColumn;
@FXML
private TableColumn<Patient, String> lastNameColumn;
@FXML
private TableColumn<Patient, String> postCodeColumn;
@FXML
private QueueTabPageController queueTabPageController;
private ObservableList<Patient> tableData;
// public static LinkedList<Patient> displayQueue;
@Override
public void initialize(URL arg0, ResourceBundle arg1)
assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file 'FXMLQueueTabPage.fxml'";
firstNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("firstName"));
lastNameColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("lastName"));
postCodeColumn.setCellValueFactory(new PropertyValueFactory<Patient, String>("postCode"));
@FXML
private void btnRefreshQueueClick(ActionEvent event) throws IOException
displayQueue(Queue.queue);
public void displayQueue(LinkedList<Patient> queue)
tableData = FXCollections.observableArrayList(queue);
tableView.setItems(tableData);
// open connection to database
conn = ConnectionFactory.getConnection();
// create statement
stmt = conn.createStatement();
// result set to pull information from database
ResultSet rs = stmt
.executeQuery("select * from queueTime where NHSNumber = '"
+ p.getNhsNumber() + "'");
while (rs.next())
// convert timestamp to String
String ts = rs.getString("Timestamp");
// print result set
System.out.print(ts + "\t");
【问题讨论】:
我没有看到任何查询??你需要的地方ResultSet rs = stmt.executeQuery("SELECT * FROM queueTime");
@brian - 感谢您的回复!我确实有一个方法,但它不允许我在 javafx 中显示它。这可能与我的表格视图有关吗? - private TableViewCURRENT_TIMESTAMP
是什么?沿着?我会发布一个小例子
【参考方案1】:
如果您将时间戳存储为 DATETIME 这是一个数字数据库字段,这可能是最简单的。这将被转换为 INT*8 bytes = long
http://www.tutorialspoint.com/sqlite/sqlite_data_types.htm
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class SQLite extends Application
public static void main(String[] args)
launch(args);
@Override
public void start(Stage stage) throws Exception
ObservableList<Person> data = FXCollections.observableArrayList();
TableView<Person> tv = new TableView<>(data);
stage.setScene(new Scene(tv,500,200));
stage.show();
TableColumn<Person, Integer> id = new TableColumn<>("id");
id.setCellValueFactory((p) -> p.getValue().id.asObject());
TableColumn<Person, String> name = new TableColumn<>("name");
name.setCellValueFactory((p) -> p.getValue().name);
TableColumn<Person, Number> epoch = new TableColumn<>("epoch");
epoch.setCellValueFactory((p) -> p.getValue().epoch);
TableColumn<Person, String> tstamp = new TableColumn<>("tstamp");
tstamp.setCellValueFactory((p) -> new ReadOnlyStringWrapper(
new Date(p.getValue().epoch.get().longValue()).toString()));
tv.getColumns().addAll(id,name,epoch,tstamp);
try (Connection con = DriverManager.getConnection("jdbc:sqlite:testtime.db");
Statement stmt = con.createStatement();)
stmt.executeUpdate("DROP TABLE IF EXISTS TEST ");
stmt.executeUpdate("CREATE TABLE TEST "
+ "(ID NUMBER PRIMARY KEY NOT NULL,"
+ " NAME TEXT, "
+ " EPOCH DATETIME)");
stmt.executeUpdate("INSERT INTO TEST VALUES"
+ "(1,'my name', " + System.currentTimeMillis() + ")");
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST");
while (rs.next())
data.add(new Person(rs.getInt("ID"),
rs.getString("NAME"),
rs.getLong("EPOCH")));
rs.close();
catch (Exception ex)
ex.printStackTrace();
System.exit(0);
class Person
SimpleIntegerProperty id;
SimpleStringProperty name;
ObjectProperty<Number> epoch;
public Person(int id, String name, long epoch)
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.epoch = new SimpleObjectProperty<>(epoch);
【讨论】:
以上是关于如何将信息添加到 sqlite 数据库,然后使用 javafx 显示信息?的主要内容,如果未能解决你的问题,请参考以下文章
使用 swiftyJSON 和 sqlite.swift 将基于 json 的数据添加到 sqlite 数据库中
应用轻量级迁移后,将 ios-add 列添加到 sqlite?