java - How can I delete/edit row in JTable and MySQL using hibernate? -
i have jtable displaying content of table in mysql database. able add record in jtable , database.. edit , delete operations possible in jtable (the changes not diplayed database). want add hibernate code in buttons events changes can displayed mysql database.
any appreciated.
here's class concerned:
package com.hibernate.stock; import java.awt.borderlayout; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.emptyborder; import javax.swing.table.defaulttablemodel; import java.awt.gridlayout; import javax.swing.jlabel; import javax.swing.jscrollpane; import javax.swing.jtextfield; import javax.swing.jtable; import javax.swing.jbutton; import org.hibernate.sqlquery; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.hibernate.cfg.configuration; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.util.arraylist; import java.util.list; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; public class gestion extends jframe { private jpanel contentpane; private jtextfield textfield; private jtextfield textfield_1; private jtextfield textfield_2; private jtextfield textfield_3; private jtable table; list biens; int i; persistantbien bien = new persistantbien(); final string columnnames[] = {"id", "nom", "catégorie", "quantité"}; final defaulttablemodel tablemodel = new defaulttablemodel(columnnames, 0); /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { gestion frame = new gestion(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. */ public gestion() { setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 450, 300); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(null); jlabel lblid = new jlabel("id:"); lblid.setbounds(12, 12, 70, 15); contentpane.add(lblid); jlabel lblnom = new jlabel("nom:"); lblnom.setbounds(12, 39, 70, 15); contentpane.add(lblnom); jlabel lblcatgorie = new jlabel("catégorie:"); lblcatgorie.setbounds(12, 69, 70, 15); contentpane.add(lblcatgorie); jlabel lblquantit = new jlabel("quantité:"); lblquantit.setbounds(12, 108, 70, 15); contentpane.add(lblquantit); textfield = new jtextfield(); textfield.setbounds(106, 10, 114, 19); contentpane.add(textfield); textfield.setcolumns(10); textfield_1 = new jtextfield(); textfield_1.setbounds(106, 37, 114, 19); contentpane.add(textfield_1); textfield_1.setcolumns(10); textfield_2 = new jtextfield(); textfield_2.setbounds(106, 67, 114, 19); contentpane.add(textfield_2); textfield_2.setcolumns(10); textfield_3 = new jtextfield(); textfield_3.setbounds(106, 106, 114, 19); contentpane.add(textfield_3); textfield_3.setcolumns(10); table = new jtable(); table.setbounds(361, 50, 1, 1); contentpane.add(table); final jscrollpane tablescrollpane = new jscrollpane(table); tablescrollpane.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { defaulttablemodel tablemodel = new defaulttablemodel(columnnames, 0); tablemodel = (defaulttablemodel) table.getmodel(); textfield.settext(tablemodel.getvalueat(table.getselectedrow(), 0).tostring()); textfield_1.settext(tablemodel.getvalueat(table.getselectedrow(), 1).tostring()); textfield_2.settext(tablemodel.getvalueat(table.getselectedrow(), 2).tostring()); textfield_3.settext(tablemodel.getvalueat(table.getselectedrow(), 3).tostring()); } }); tablescrollpane.setbounds(240, 11, 198, 135); contentpane.add(tablescrollpane); jbutton btnajouter = new jbutton("ajouter"); btnajouter.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { configuration cfg = new configuration(); cfg.configure("hibernate.cfg.xml"); sessionfactory sf = cfg.buildsessionfactory(); session s = sf.opensession(); transaction tx = s.begintransaction(); bien.setid_article(textfield.gettext()); bien.setnom_article(textfield_1.gettext()); bien.setcategorie(textfield_2.gettext()); bien.setquantite(textfield_3.gettext()); s.save(bien); s.flush(); tx.commit(); s.close(); } }); btnajouter.setbounds(12, 158, 117, 25); contentpane.add(btnajouter); jbutton btnediter = new jbutton("editer"); btnediter.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { configuration cfg = new configuration(); cfg.configure("hibernate.cfg.xml"); sessionfactory sf = cfg.buildsessionfactory(); session s = sf.opensession(); transaction tx = s.begintransaction(); defaulttablemodel tablemodel = new defaulttablemodel(columnnames, 0); tablemodel = (defaulttablemodel) table.getmodel(); tablemodel.setvalueat(textfield.gettext(), table.getselectedrow(), 0); tablemodel.setvalueat(textfield_1.gettext(), table.getselectedrow(), 1); tablemodel.setvalueat(textfield_2.gettext(), table.getselectedrow(), 2); tablemodel.setvalueat(textfield_3.gettext(), table.getselectedrow(), 3); s.save(bien); s.flush(); tx.commit(); s.close(); } }); btnediter.setbounds(150, 158, 117, 25); contentpane.add(btnediter); jbutton btnsupprimer = new jbutton("supprimer"); btnsupprimer.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { configuration cfg = new configuration(); cfg.configure("hibernate.cfg.xml"); sessionfactory sf = cfg.buildsessionfactory(); session s = sf.opensession(); transaction tx = s.begintransaction(); defaulttablemodel tablemodel = new defaulttablemodel(columnnames, 0); tablemodel = (defaulttablemodel) table.getmodel(); tablemodel.removerow(table.getselectedrow()); sqlquery query=s.createsqlquery("delete * tbiens id-article='"+textfield.gettext()+"'"); s.flush(); tx.commit(); s.close(); } }); btnsupprimer.setbounds(303, 158, 117, 25); contentpane.add(btnsupprimer); jbutton btnafficher = new jbutton("afficher"); btnafficher.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { try{ configuration cfg = new configuration(); cfg.configure("hibernate.cfg.xml"); sessionfactory sf = cfg.buildsessionfactory(); session s = sf.opensession(); transaction tx = s.begintransaction(); sqlquery query=s.createsqlquery("select * tbiens"); biens = query.list(); arraylist<object[]> res = new arraylist<object[]>(biens); final defaulttablemodel tablemodel = new defaulttablemodel(columnnames, 0); table.setmodel(tablemodel); (final object[] bien : res) { // assuming each row in biens list list of strings... final object[] row = bien; tablemodel.addrow(row); } biens.size(); system.out.print(i); s.flush(); tx.commit(); s.close(); } catch (classcastexception e) { e.printstacktrace(); } } }); btnafficher.setbounds(166, 235, 117, 25); contentpane.add(btnafficher); }
}
output:
editing
by default, defaulttablemodel
make cells editable, double click cell want change , enter "edit" model.
to save values database depend on approach want take, override setvalueat
of tablemodel
method , push changes when method called, personally, i'd add save button , push changes batch
deleting
this little more difficult. problem is, once remove 1 row, indices other selected items change...
a better solution devise hibernate bean/data class , load via hibernate. use custom implementation of tablemodel
, extending abstracttablemodel
, give management control of content.
you then, objects selected (this method in custom tablemodel
, getvalueat(int row)
returned hibernate object @ specified row) , pass kind of delete method (ie removevalue(tbiens bean)
), firing appropriate event notifications...
Comments
Post a Comment