java - How to update changed data into the Database by using JTable? -


hello stackoverflow users.

i ask couple of questions.

i able data database , display in jtable. in addtion, update data in database has been modified in jtable don't know how it. found of examples other people did did not work me.

so question this: matter how getting , storing data database?? using array of string

public string getdbunits  [][];  

to store , pass data database , converting array in vector , display data using

table.setmodel(new defaulttablemodel(getdbgrades,gradeheader)); 

for jtable.

i know different methods can used don't know 1 better useful situation.

i highly appreciated advice or tutorial.

thanks.

ducky :)

p.s. if 1 need code of methods use data database happily post them.

this open ended question.

for me, have few options...

you could...

update database in real time data changed within table.

this require either extend tablemodel have access setvalue method, allow not update state of model, update state of database (i'd update database , when successful, update model, that's me) or attach listener model , monitor approriate tablechanged events , update database accordingly.

while looks nice method, run risk of "hanging" ui while database been updated or getting out of sync what's in model , what's in database...

you could...

create class represents data database. when value changed within object, raise flag stating row has changed.

when user has finished making changes table data, click "save". walk through objects , determine ones need updated , "save" them database.

this have potential allow "stale" data written database (ie user loads data , starts making changes. user b in meantime loads data, makes changes , saves database. user saves changes, overwriting of changes user b has committed)

there approaches solving both, need decide on approach best suits current problem, real-time or delayed updates...

updated basic example

the first thing start creating class represents data database. normally, i'd start interface , use kind of factory generate actual implementation, small steps...

this has 1 important variable, haschanged. used flag object has needing update...

public class person {      private boolean haschanged = false;      private string firstname;     private string lastname;      public person(string firstname, string lastname) {         this.firstname = firstname;         this.lastname = lastname;     }      public boolean haschanged() {         return haschanged;     }      public string getfirstname() {         return firstname;     }      public string getlastname() {         return lastname;     }      public void setfirstname(string value) {         if (value == null ? firstname != null : !value.equals(firstname)) {             firstname = value;             haschanged = true;         }     }      public void setlastname(string value) {         if (value == null ? lastname != null : !value.equals(lastname)) {             lastname = value;             haschanged = true;         }     } } 

next built table model capable of modeling list of of objects...

public class peopletablemodel extends abstracttablemodel {      private list<person> people;      public peopletablemodel() {         people = new arraylist<>(20);     }      public void addperson(person person) {         people.add(person);         firetablerowsinserted(people.size() - 1, people.size() - 1);     }      public person getpersonat(int row) {         return people.get(row);     }      public list<person> getchangedpeople() {         list<person> changed = new arraylist<>(people.size());          (person p : people) {             if (p.haschanged()) {                 changed.add(p);             }         }          return changed;         }      @override     public int getrowcount() {         return people.size();     }      @override     public string getcolumnname(int column) {         string name = null;         switch (column) {             case 0:                 name = "first name";                 break;             case 1:                 name = "first name";                 break;         }         return name;     }      @override     public int getcolumncount() {         return 2;     }      @override     public object getvalueat(int rowindex, int columnindex) {         person p = people.get(rowindex);         object value = null;         switch (columnindex) {             case 0:                 value = p.getfirstname();                 break;             case 1:                 value = p.getlastname();                 break;         }         return value;     }      @override     public void setvalueat(object avalue, int rowindex, int columnindex) {         if (avalue instanceof string) {             person p = people.get(rowindex);             switch (columnindex) {                 case 0:                     p.setfirstname(avalue.tostring());                     break;                 case 1:                     p.setlastname(avalue.tostring());                     break;             }             firetablerowsupdated(rowindex, rowindex);         }     } } 

there number of ways achieve same thing, here i've provided method called getchangedpeople return people have changed. loop through list , call appropriate database update statement.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -