java - JTable , update the table whenever click on a button -
i trying learn jtable mysql. i've set database jtable , added gui using scroll pane.
my question is, how can make function renew database or search operation (both same thing so..) whenever click on button.
here code (i reading database informations .ini file)
final vector columnnames = new vector(); final vector data = new vector(); try { // connect access database driver = inifonksiyon.inisvnokut(driver, "databasebilgileri", "driver");//"com.mysql.jdbc.driver"; url = inifonksiyon.iniokut(url, "databasebilgileri", "url");//"jdbc:mysql://localhost:3306/"; userid = inifonksiyon.iniokut(userid, "databasebilgileri", "kullanici"); password = inifonksiyon.iniokut(password, "databasebilgileri", "sifre"); dbname = inifonksiyon.iniokut(dbname, "databasebilgileri", "dbismi"); class.forname( driver ); connection connection = drivermanager.getconnection( url+dbname , userid, password ); // read data table string sql = "select * profildb.tbl_detailed"; statement stmt = connection.createstatement(); resultset rs = stmt.executequery( sql ); resultsetmetadata md = rs.getmetadata(); int columns = md.getcolumncount(); // column names (int = 1; <= columns; i++) { columnnames.addelement( md.getcolumnname(i) ); } // row data while (rs.next()) { vector row = new vector(columns); (int = 1; <= columns; i++) { row.addelement( rs.getobject(i) ); } data.addelement( row ); } rs.close(); stmt.close(); connection.close(); } catch(exception e) { system.out.println( e ); } // create table database data final jtable table = new jtable(data, columnnames) { /** * */ private static final long serialversionuid = 1l; public class getcolumnclass(int column) { (int row = 0; row < getrowcount(); row++) { object o = getvalueat(row, column); if (o != null) { return o.getclass(); } } return object.class; } };
this code handles creating table, directly connected main gui function, how can refresh/do operation search function?
it easier use table model. see related tutorial. can use defaulttablemodel or own implementation of tablemodel
. extension of abstracttablemodel
common approach if defaulttablemodel
not satisfy needs.
for better performance , responsiveness of swing application execution of long running tasks should not happen on edt. so, best access database thread other edt.
swingworker can used such purpose. data database in swingworker.doinbackground()
. then, either build or update model in swingworker.done()
.
there many variations of such implementation. these specific application's nature , requirements. instance can use swingworker's publish()/process()
tandem process data in chunks. may choose incorporate database access logic model or create commands extend swingworker
.
Comments
Post a Comment