java - Populate a tableview using database in JavaFX -
i'm starting learn javafx , need populate table data database. i've read lot of code online, haven't found looking for. read this don't know how implement last function. read other code , far of code:
@fxml private tableview<user> table; @fxml private tablecolumn<user, string> namecol; @fxml private tablecolumn<user, string> emailcol; private observablelist<user> data; public void initialize(url location, resourcebundle resources) { namecol.setcellvaluefactory(new propertyvaluefactory(“name”)); emailcol.setcellvaluefactory(new propertyvaluefactory(“email”)); builddata(); } public void builddata() { connection connect = new connection(); statement st = connect.connect(); data = fxcollections.observablearraylist(); try { resultset rs = st.executequery("select * user"); while (rs.next()) { observablelist<user> row = fxcollections.observablearraylist(); (int = 1; <= rs.getmetadata().getcolumncount(); i++) { row.add(rs.getstring(i)); system.out.println(row); } data.add(pol); } tabla.setitems(data); } catch (sqlexception ex) { joptionpane.showmessagedialog(null, ex); } }
i hope can me
i'm sure you:
public class dbclass{ public connection getconnection() throws classnotfoundexception, sqlexception{ class.forname("com.mysql.jdbc.driver"); return drivermanager.getconnection("jdbc:mysql://192.168.0.1:3306/dbname","mysqluser","mysqluserpwd"); } }
in controller class following :
@fxml void initialize(){ assert tableview != null : "fx:id=\"tableview\" not injected: check fxml file 'usermaster.fxml'."; colusername.setcellvaluefactory( new propertyvaluefactory<usermaster,string>("username")); colpassword.setcellvaluefactory( new propertyvaluefactory<usermaster,string>("userpassword")); colusertype.setcellvaluefactory( new propertyvaluefactory<usermaster,string>("usertype")); colphoto.setcellvaluefactory( new propertyvaluefactory<object,imageview>("userphoto")); objdbclass = new dbclass(); try{ con = objdbclass.getconnection(); builddata(); } catch(classnotfoundexception ce){ logger.info(ce.tostring()); } catch(sqlexception ce){ logger.info(ce.tostring()); } } private observablelist<usermaster> data; public void builddata(){ data = fxcollections.observablearraylist(); try{ string sql = "select * usermaster order username"; resultset rs = con.createstatement().executequery(sql); while(rs.next()){ usermaster cm = new usermaster(); cm.userid.set(rs.getint("userid")); image img = new image("tailoring/userphoto/user"+cm.getuserid().tostring()+".jpg"); imageview mv = new imageview(); mv.setimage(img); mv.setfitwidth(70); mv.setfitheight(80); cm.userphoto.set(mv); cm.username.set(rs.getstring("username")); cm.userpassword.set(rs.getstring("userpassword")); cm.usertype.set(rs.getstring("usertype")); data.add(cm); } tableview.setitems(data); } catch(exception e){ e.printstacktrace(); system.out.println("error on building data"); } }
and create pojo seperate java file every entity(table) want manipulate using tableview
public class usermaster{ public simpleintegerproperty userid = new simpleintegerproperty(); public objectproperty userphoto = new simpleobjectproperty(); public simplestringproperty username = new simplestringproperty(); public simplestringproperty userpassword = new simplestringproperty(); public simplestringproperty usertype = new simplestringproperty(); public simplestringproperty encpass = new simplestringproperty(); public simplestringproperty updt = new simplestringproperty(); public simplestringproperty uptm = new simplestringproperty(); public integer getuserid() { return userid.get(); } public object getuserphoto() { return userphoto.get(); } public string getusername() { return username.get(); } public string getuserpassword() { return userpassword.get(); } public string getusertype() { return usertype.get(); } public string getencpass() { return encpass.get(); } public string getupdt() { return updt.get(); } public string getuptm() { return uptm.get(); } }
i have tested program working perfectly.
Comments
Post a Comment