• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add selected row from jtable to database  RSS feed

 
Vlad Stan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, how exactly can you add selected row to database and how can you check if the ID is not in the database(ID is unique)?
Atm i'm taking everything from jtable to database.


This is the code for selected row :
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atm i'm taking everything from jtable to database.


Why?

If you requirement is to only take the selection row, why would you loop through all rows???

Just get rid of the outer loop and use the index of the selected row:

Also, you can get the data directly from the table so you don't need to worry about converting the selected index

//Object o = tm.getValueAt(...);
Object o = table.getValueAt(...);


You would only need to covert the view index to the model index if you want to delete the row from the model. But I don't know why you would want to delete the row from the table.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vlad Stan wrote:how can you check if the ID is not in the database(ID is unique)?


MySQL has syntax like INSERT ... ON DUPLICATE KEY UPDATE. (It assumes that you have a unique key column declared in the table.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!