• Post Reply Bookmark Topic Watch Topic
  • New Topic

inserting jtable into database

 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends..i m using javax.swing.JTable..with defalutSetModel..it has three columns..i want to insert it into the mysql database.can i insert whole table in one column of database..please help me..
i m new to usiing the swings components
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to store the entire JTable, with all rows and columns, into one column?

Do you want to store each JTable row in a database row, or do you want to store all JTable rows into one single database row?

If you want to store the entire JTable into one row, you can serialize the JTable's TableModel into the database. Setting it using a PreparedStatement stmt:
Retrieving it from a ResultSet rs:

If you want to store each JTable row in a separate database row, you should create some object containing all the data (perhaps an Object[] or ArrayList<Object>), then store and retrieve that similar to the above.
In both cases the column must have a BLOB type.


A final word: I think it is bad design to merge objects from different rows (and perhaps even columns) into one single column (perhaps even cell). A JTable has a clear tabular structure, database tables have a very very similar tabular structure. You can map these one one one if you if the number of columns matches.
 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot rob for quick response..bt i m in trap that i want to save the row wise data of table into the database..bt there is a problem that in this case there is violation of rule of unique primary key..if you have any other idea to save this data in one unique id in database..then it will be very helpful to me
thanks in advance..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!