• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable & Database , design question, what do you think?

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Q: I have a JTable 5x10 I need to insert the content into a database (and later on retrieve it). Every record has a JTable and every record is unique (there are many records) the JTable will have its own table in the database (segmentsTable) and a KEY.

So: how do I place all the content into the table???

my solution, and please feel free to criticize this idea, is to get all information of each raw and insert it to the db.

like this:

public void save()
{
int row = table.getSelectedRow();
String col0 = table.getValueAt(row, 0).toString(); // Value of 0 is in this Variable
String col1 = table.getValueAt(row, 1).toString(); // Value of 1 is in this Variable
// and so on

// Do insert KEY + raw content
}

so basically I'll have to have a table with 10 attributes and each attribute will contain a raw.

What do you think? I am very new to db so I'm not sure this is the most efficient way to do this. if you know a better way to insert/update/select info from a jtable PLEAES let me know.

Thanks
Ppr
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
I'm a bit confused by the question. Is it that each row in the JTable corresponds to a row in the database? Or that there are multiple JTables?
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here are multiple JTables?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a JTable, you usally use a TableModel, and the JTable has to be in sync with the TableModel (with setValueAt).
When you store the data, you store the data from the TableModel (which is in sync with the JTable - which is a View on the data), and don't query the JTable.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS Stefan Wagner
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our Swing/AWT/SWT/JFace forum for filing because the question is more about JTables than JDBC>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic