• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Insert data from JTable to existing MySQL table

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have been searching for a while now on how to save JTable data, how to make buttons to do so, and importing data to a MySQL table.

However, what I have managed to complete is quite far from the goal.

What I want to be able to do is edit the JTable inside of the JFrame and then save those changes to a the selected table, which the user will have clicked on via a JComboBox.

What I have working at the moment is a "MainFrame" that shows the combobox containing the schema's table names, and a preview button that opens a new frame with the JTable containing the selected tables data.

Currently, when I press on the SaveButton and then preview the file again, it saves the data inside the JTable, but it isn't writing the data to the MySQL database.
Also, when I press preview again, a new set of blank columns come up to the right, and a new set of data (from the MySQL table) is appended beneath the edited data.

Thanks in advance.


 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 104 will result in an invalid SQL statement. If the table is "sometable" the it produces the string "TRUNCATE TABLEsometable". Same kind of problem with the next line.
 
Daniel Lawton
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Junilu, I will fix this and try it out.
 
Daniel Lawton
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Line 104 will result in an invalid SQL statement. If the table is "sometable" the it produces the string "TRUNCATE TABLEsometable". Same kind of problem with the next line.



Upon fixing this, in the preview window I can still change the data, and when the save button is pressed, and I go to look at the MySQL table, nothing changes. If I press Preview again, to bring up the new JFrame containing the JTable data, i get an extra 3 blank columns, with the original MySQL table data appended on the bottom of it. It does however show the changes made from before.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sort of code shoul‍d not be in a frame or any other display class. I think you need to develop your app in about four stages:-
  • 1: As a bare database, so you know all yoru updates are working. Obviously you record the SQL for future reference, avoiding TRUNCATE statements.
  • 2: Write a Java® app which runs the database and the queries, using the command line to enter the data (not the whole SQL which shoul‍d be encapsulated in your methods).
  • 3: Write your table and the table model.
  • 4: Use the listeners to join stage 2 and stage 3 together.
  • Remind yourself of some of those things in the Java™ Tutorials (1) and (2). You can print the SQL to the command line with System.out.println so you can see the sort of errors Junilu pointed out. Remove the print instructions when you have got it all working.
     
    Daniel Lawton
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for taking the time to write that detailed response, I'll have a look into it.
     
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How are you running this?
    I ask, because you should be seeing errors somewhere from that SQLException block.
     
    Daniel Lawton
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:How are you running this?
    I ask, because you should be seeing errors somewhere from that SQLException block.



    Just by pressing the green play button at the top of eclipse. There is one "Safety type" error.
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OK, so previous to correcting the SQL syntax you presumably got a SQLException being thrown and printed to the IDE console?

    Ah!
    I misread the catch block.
    That should say:


    This way you'll get all the important information about what has gone wrong and exactly where.
     
    Daniel Lawton
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:OK, so previous to correcting the SQL syntax you presumably got a SQLException being thrown and printed to the IDE console?

    Ah!
    I misread the catch block.
    That should say:


    This way you'll get all the important information about what has gone wrong and exactly where.



    Nothing comes up in the console when the code is run!
     
    Dave Tolls
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Then you probably ought to put in some logging statements so you can debug where your code is going.
    Simple System.out.println() ones with suitable messages in them, including possibly useful variable values you might want to track.
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's possible that getRowCount() and/or getColumnCount() of changedTableModel is returning 0. Did you double check that?
     
    Daniel Lawton
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:It's possible that getRowCount() and/or getColumnCount() of changedTableModel is returning 0. Did you double check that?



    So, I have added in a couple of system.out.println to check the value of column and row.

    Literally nothing comes up.

    I guess this means that the class isn't being used at all?

     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Your check for columns won't print anything if getColumnCount is zero because you'll never enter the loop. Put the println statement outside the loop. If you still don't get output, then that section of code is not getting executed at all.

    And print out the value of changedTableModel.getColumnCount(), not column.
     
    Daniel Lawton
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:Your check for columns won't print anything if getColumnCount is zero because you'll never enter the loop. Put the println statement outside the loop. If you still don't get output, then that section of code is not getting executed at all.

    And print out the value of changedTableModel.getColumnCount(), not column.



    It should come up in the console when I edit the cell and press enter I think?

    However, nothing does...
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah, see this is why I don't like yuge methods, they hide disconnects between the intent and what's actually being done; they mislead you almost every time.

    Line 5 in your last reply.  What you're doing there is adding a listener.  That's it.  That's like saying "Ok, stand there and wait for something to happen, then do all this stuff."

    Your code is still there standing there. Nothing has happened to actually make it execute.  That is, nothing has happened that would trigger the table model to call the tableChanged() method of your listener.
     
    Daniel Lawton
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:Ah, see this is why I don't like yuge methods, they hide disconnects between the intent and what's actually being done; they mislead you almost every time.

    Line 5 in your last reply.  What you're doing there is adding a listener.  That's it.  That's like saying "Ok, stand there and wait for something to happen, then do all this stuff."

    Your code is still there standing there. Nothing has happened to actually make it execute.  That is, nothing has happened that would trigger the table model to call the tableChanged() method of your listener.



    I will look into the documentation for TableModelListener. I thought surely it should count as an event if you changed the table in the jframe but perhaps not.
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're not following standard naming conventions and this makes your code more difficult to read. Only constructors and class/interface names should be capitalized. Method and variable names should be camelcase starting with a lowercase letter.
     
    Daniel Lawton
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:You're not following standard naming conventions and this makes your code more difficult to read. Only constructors and class/interface names should be capitalized. Method and variable names should be camelcase starting with a lowercase letter.



    Apologies for that,

    I think I might have found my issue.

    No where in my code am I using "setValueAt" so the table isn't actually changing any values. The only reason the changed cells where still there when I opened preview again was due to JFrame remembering them? or something idk.

    Either way, I'm not too sure where I would tell it to change the value. I think I would have to loop through the entire table to set the values to whatever they were changed to? I'd have to do this inside the PreviewButton method. (Again, apologies for the naming convention)
     
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is this solved?
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic