aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes SQL Server Table in JTable - Results Editable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "SQL Server Table in JTable - Results Editable" Watch "SQL Server Table in JTable - Results Editable" New topic
Forums: Swing / AWT / SWT JDBC
Author

SQL Server Table in JTable - Results Editable

Gwen Bobbie
Greenhorn

Joined: May 29, 2014
Posts: 3
Hello. I am new to java so please be kind. I am attempting to show a GUI that will show records from a SQL Server table. That part I have done. However, I need to allow the user to be able to edit the Approval field of the table when he/she views it. Following is my code:


Any assistance is GREATLY appreciated!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
I know precious little about JTables, but I shall move the discussion to the GUIs forum where we usually discuss such things. Maybe to a databases forum, too. I don't know whether there is anything useful in the Java Tutorials.
Do you need .newInstance() in line number 16? I thought you got the connection automatically because there is a static initialiser in the driver class which does the connecting.
Use a finally (or better in Java7 try‑with‑resources) to close the database connection.

And welcome to the Ranch
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
Well, the short answer is that you can edit a cell value by double-clicking on the cell you want to edit and typing in a new value, but a more complete -- and probably more useful -- answer requires more information:

  • What type of data is contained within the "Approval" field (column)? For example, is it a binary (yes/no) type of value? Free-form text? Selection from a finite list of possible values?
  • The way your post is worded implies that the other columns are supposed to be read-only. Is that correct or does it matter whether or not the other column values can be edited?
  • Gwen Bobbie
    Greenhorn

    Joined: May 29, 2014
    Posts: 3
    Yes, you can double click and edit the cell itself, but that does not update the database, which is what I am looking for. It is okay if all of the cells are available for editing, I should have rephrased that. I just need the edits done to go back to the db. The fields are text.
    Brett Spell
    Ranch Hand

    Joined: Feb 24, 2001
    Posts: 96
    Are you asking how write the code that updates the database, how to have that code called, or both? They're two different questions and the answer to the first would be better suited to the forum that handles database questions. This is the right forum for the second question (how to have the code triggered / called that performs the database update), though.

    To be notified that the data has been changed you'll need to define and register a listener, which is basically just an object that gets notified when something happens: in this case when a cell has been edited. For your listener you'll want to take a look at addTableModelListener() in the TableModel class. The TableModel is an object that contains the data you created and stored in vectors; in this case it was created for you when you passed in the data Vector to the table constructor, and you can get a reference to it by calling getModel() on the JTable instance you created.

    Here are the basic steps you'll need to follow:

  • Create a TableModelListener
  • Register it with your table's TableModel
  • When tableChanged() gets called indicating that a cell value was edited, call your code that updates the value in the database
  • Gwen Bobbie
    Greenhorn

    Joined: May 29, 2014
    Posts: 3
    Extremely helpful, and just what I was looking for to get started. Thank you!!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38353
        
      23
    Brett Spell wrote: . . . would be better suited to the forum that handles database questions. . . .
    As far as I know, this discussion is in both fora.

    Thank you for the suggestion about table model listeners. You can read about them in the Java Tutorials. It seems to be a very brief section.
    Brett Spell
    Ranch Hand

    Joined: Feb 24, 2001
    Posts: 96
    Campbell Ritchie wrote:Thank you for the suggestion about table model listeners.


    You're welcome.

    Campbell Ritchie wrote:You can read about them in the Java Tutorials. It seems to be a very brief section.


    I'll pass, thanks; I'm already pretty comfortable with using the various JTable-related listeners.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38353
        
      23
    Actually, it wasn't you I intended the link for: I was by no means clear, was I?
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    Campbell Ritchie wrote:Do you need .newInstance() in line number 16? I thought you got the connection automatically because there is a static initialiser in the driver class which does the connecting.


    Yes, that's correct. But people put in the .newInstance() call because they've been reading old tutorials, or copying old code. History: The Microsoft JVM had a bug in it whereby the static initializer business didn't work properly and you actually had to create an instance of the driver class. And therefore many people posted code on the web which did that. A small fraction of those people actually explained why, but the rest didn't bother. The Microsoft JVM hasn't been in use since Microsoft lost the court case back in 2004, but the unnecessary code lives on.

    However it seems to me that the EU just mandated Google to remove references to outdated pages on request. Maybe we could make a list of outdated pages which use .newInstance() on a JDBC driver class and submit it to Google for them to ignore?

    (On a more serious note, creating a new instance of the driver class does nothing harmful at all.)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: SQL Server Table in JTable - Results Editable