如何将信息添加到sqlite数据库,然后使用javafx显示信息?

时间:2015-04-22 15:21:52

标签: java database sqlite jdbc javafx

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");

1 个答案:

答案 0 :(得分:1)

如果将时间戳存储为DATETIME(数字数据库字段),则可能最简单。这将转换为INT * 8字节=长

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);
        }

    }
}