java - How to add information to a sqlite database and then display the information using javafx? -
public static void addtoqueuetable(patient p) { try { // open connection database conn = connectionfactory.getconnection(); // create statement stmt = conn.createstatement(); string sql = "insert queuetime values('" + p.getnhsnumber() + "', current_timestamp)"; // execute update stmt.executeupdate(sql); } catch (exception e) { e.printstacktrace(); system.out.println("error on building data"); } { // close open resources in database dbutil.close(stmt); dbutil.close(conn); } }
the above code add details (nhsnumber , current timestamp) database - wondering how display using javafx?
patient class follows:
public patient(string nhsnumber, string firstname, string lastname, string postcode) { super(nhsnumber,firstname, lastname , postcode); }
with javafx able display details (see below), i'm not sure how can timestamp database well? appreciated!
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\" not injected: check 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 database conn = connectionfactory.getconnection(); // create statement stmt = conn.createstatement(); // result set pull information database resultset rs = stmt .executequery("select * queuetime nhsnumber = '" + p.getnhsnumber() + "'"); while (rs.next()) { // convert timestamp string string ts = rs.getstring("timestamp"); // print result set system.out.print(ts + "\t");
it's easiest if store timestamp datetime numeric database field. converted 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 test values" + "(1,'my name', " + system.currenttimemillis() + ")"); resultset rs = stmt.executequery("select * 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); } } }
Comments
Post a Comment