java - how do i update my jtable after i add or delete my item -
im developing database project. jtextfield, addbutton, editbutton , jtable on 1 jframe. when made addbutton open jframe , place items added there, works not update jtable, jtable updates after make action editing or adding item, should problem here? here example of edit button. btw need inserted , updated mysql
if(editid.gettext() != null) { string updatequery = null; preparedstatement ps = null; connection con = main.getconnection(); try{ updatequery = "update mydatabase set name = ?, price = ?" + ", quantity = ? id = ?"; ps = con.preparestatement(updatequery); ps.setstring(1, editname.gettext()); ps.setstring(2, editprice.gettext()); ps.setstring(3, editquantity.gettext()); ps.setstring(4, editid.gettext()); ps.executeupdate(); main.getproductlist(); main.show_products_in_jtable(); joptionpane.showmessagedialog(null, "product updated"); }catch(exception ex) { joptionpane.showmessagedialog(null, ex.getmessage()); } }else{ joptionpane.showmessagedialog(null, "one or more fields empty or wrong"); }
so here 2 methods used edit table... seems incomplete, need method in main simultaneously refresh jtable. brain exploding right problem. cause cant see problem why moving edit method jframe affect program.
public arraylist<database1> getproductlist() { arraylist<database1> productlist = new arraylist<database1>(); connection con = getconnection(); string query = "select * mydatabase"; statement st; resultset rs; try { st = con.createstatement(); rs = st.executequery(query); database1 mydatabase; while(rs.next()) { mydatabase = new database1(rs.getstring("id"),rs.getstring("name"),rs.getint("price"),rs.getint("quantity")); productlist.add(mydatabase); } } catch (sqlexception ex) { logger.getlogger(mainwindow.class.getname()).log(level.severe, null, ex); } return productlist; } public void show_products_in_jtable() { arraylist<database1> list = getproductlist(); defaulttablemodel model = (defaulttablemodel)itemtable.getmodel(); model.setrowcount(0); object[] row = new object[4]; for(int = 0; < list.size(); i++) { row[0] = list.get(i).getid(); row[1] = list.get(i).getname(); row[2] = list.get(i).getquantity(); row[3] = list.get(i).getprice(); model.addrow(row); } itemtable.setmodel(model); model.firetabledatachanged(); }
Comments
Post a Comment