Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Loading different JTables to different MySQL Tables

Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am trying to make a program that allows the user to select a table out of a certain MySQL schema, preview it, edit it (edit cell, delete row, add row) and then save the table back into the MySQL table.

I have managed to achieve this for one table. I now need to make it so it can handle the other tables. The main issue with this is that the tables have different column names/column counts - at the moment I am grabbing the values from column 0,1,2 and putting them into the correct columns in the INSERT statement (LoadID,Active,LoadPath)

I don't know how I would make it so that it gets the column count and then changes the column names in the INSERT INTO statement.

Posts: 4495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loop over the column names in the table model to build the initial INSERT (and I would use a PreparedStatement).

Something along those lines.
As it stands that'll give you excess ',', but (ignoring that for the moment) it should give you:
INSERT INTO <your table> (col1, col2, col3 etc) VALUES (?,?,?,? etc)

It's the sort of thing that needs to be in its own method so you can concentrate on getting it right without worrying about actually running it.
Then the final string can be used with a prepared statement.
That last part is important.

Finally, you can then look at doing it as a batch call rather than several separate INSERTS.
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!