2015-04-22 80 views
2
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?

Patient类如下:如何将信息添加到sqlite数据库,然后使用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

我看不出有任何疑问?某处你需要'ResultSet rs = stmt.executeQuery(“SELECT * FROM queueTime”);' – brian

+0

@brian - 谢谢你的回复!我确实有这样的方法,但它不让我在javafx中显示它。这可能与我的tableview有关吗? - private TableView tableView; ...目前它直接从患者对象取值,而不是queueTime表... – Davidaj

+0

将结果集添加到可观察列表的代码在哪里?我也没有看到Patient类中的时间字段。 – brian

回答

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

    } 
} 
相关问题