Thursday, 15 April 2010

Using java swing table to update MySQL database values -



Using java swing table to update MySQL database values -

this programme used read info database. in database,there 3 tables pki17, pki18, pkn18. viewing different tables,jcombobox used , works changing tablemodel of table. using tablechanged method made table editable. when cell value in table changed corresponding value in database has alter to.

when utilize tablechanged , actionperformed methods together, value in database doesn’t changed when i’m editing cell in swing table. when remove actionperformed method, can update database editing table cells.

i need have both abilities, take table database using jcombobox , update database values editing values in swing table.

i think problem exists because tablemodel of table changed in both methods. don’t know how solve it.

public class tablecombobox extends jpanel implements actionlistener, tablemodellistener { static jtable table; static jcombobox box; static mytablemodel model; static connection con = null; static statement stmt = null; public tablecombobox() throws sqlexception { super(new borderlayout()); table = new jtable(new mytablemodel("pki18")); table.setpreferredscrollableviewportsize(new dimension(500, 400)); table.setfillsviewportheight(true); table.getmodel().addtablemodellistener(this); jscrollpane scrollpane = new jscrollpane(table); jpanel menupanel = new jpanel(); menupanel.setlayout(new boxlayout(menupanel, boxlayout.y_axis)); menupanel.setborder(borderfactory.creatematteborder(0, 0, 0, 1, color.black)); string[] dalykas = { "chose groop", "pki17", "pki18", "pkn18" }; box = new jcombobox(dalykas); box.setmaximumsize(new dimension(150, 25)); box.setalignmentx(component.left_alignment); box.addactionlistener(this); box.setselectedindex(2); menupanel.add(box); jpanel cards = new jpanel(new cardlayout()); cards.add(scrollpane, "view"); add(menupanel, borderlayout.line_start); add(cards, borderlayout.center); } public void tablechanged(tablemodelevent e) { int row = e.getfirstrow(); int col = e.getcolumn(); model = (mytablemodel) e.getsource(); string stulppav = model.getcolumnname(col); object info = model.getvalueat(row, col); object studid = model.getvalueat(row, 0); system.out.println("tablechanded works"); seek { new importdata(stulppav, data, studid); } grab (classnotfoundexception e1) { e1.printstacktrace(); } grab (sqlexception e1) { e1.printstacktrace(); } } public void actionperformed(actionevent event) { jcombobox cb = (jcombobox) event.getsource(); string pav = (string) cb.getselecteditem(); if (pav != "chose groop") { seek { model = new mytablemodel(pav); table.setmodel(model); } grab (sqlexception e) { e.printstacktrace(); } } } private static void gui() throws sqlexception { jframe frame = new jframe("e-gradebook"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setcontentpane(new tablecombobox()); frame.pack(); frame.setsize(800, 400); frame.setvisible(true); } public static void main(string[] args) throws sqlexception { seek { class.forname("com.mysql.jdbc.driver"); con = drivermanager.getconnection("jdbc:mysql://localhost/pki18", "root", ""); gui(); } grab (sqlexception e) { e.printstacktrace(); } grab (classnotfoundexception e1) { e1.printstacktrace(); } { if (stmt != null) stmt.close(); } } static connection getconnection() { homecoming con; } } public class importdata { static connection con = tablecombobox.getconnection(); public importdata(string a, object b, object c) throws classnotfoundexception, sqlexception { statement stmt = null; seek { string stulppav = a; string duom = b.tostring(); string studid = c.tostring(); system.out.println(duom); con.setautocommit(false); stmt = con.createstatement(); stmt.addbatch("update pki18 set " + stulppav + " = " + duom + " studento_id = " + studid + ";"); stmt.executebatch(); con.commit(); } grab (batchupdateexception e) { e.printstacktrace(); } grab (sqlexception e) { e.printstacktrace(); } { if (stmt != null) stmt.close(); con.setautocommit(true); system.out.println("data imported database"); } } } public class mytablemodel extends abstracttablemodel{ static int rowcount; static object info [][]; static string columnnames []; public mytablemodel(string grupname) throws sqlexception{ string query ="select studento_id, vardas_pavarde, 1_semestras,"+ " 2_semestras, egzaminas, bendras_balas "+ "from pki18." + grupname; resultset rs ; connection con = tablecombobox.getconnection(); statement stmt = null; stmt = con.createstatement(); rs = stmt.executequery(query); rs.last(); rowcount = rs.getrow(); info = new object[rowcount][6]; rs = stmt.executequery(query); (int ieil = 0; ieil < rowcount; ieil++){ rs.next(); data[ieil][0] = rs.getlong("studento_id"); data[ieil][1] = rs.getstring("vardas_pavarde"); data[ieil][2] = rs.getbyte("1_semestras"); data[ieil][3] = rs.getbyte("2_semestras"); data[ieil][4] = rs.getbyte("egzaminas"); data[ieil][5] = rs.getbyte("bendras_balas"); } string[] columnname = {"asmens_kodas","vardas_pavarde","1_semestras" ,"2_semestras","egzaminas","bendras_balas"}; columnnames = columnname; } public int getcolumncount(){ homecoming columnnames.length; } public int getrowcount(){ homecoming data.length; } public string getcolumnname(int col){ homecoming columnnames[col]; } public object getvalueat(int row, int col){ homecoming data[row][col]; } public class getcolumnclass(int col){ homecoming getvalueat(0, col).getclass(); } public boolean iscelleditable(int row, int col){ homecoming true; } public void setvalueat(object value, int row, int col){ data[row][col] = value; firetablecellupdated(row, col); } }

in constructor, add together table model listener current model only:

class="lang-java prettyprint-override">table.getmodel().addtablemodellistener(this);

in action event, however, replace table model:

class="lang-java prettyprint-override">model = new mytablemodel(pav); table.setmodel(model);

as consequence, new table model won't have listener, , won't receive notifications more. have actionperformed method add together listener well, , problem should fixed.

java mysql swing jtable tablemodel

No comments:

Post a Comment