I got this inquiry recently. I decided to try it myself. Here, I am showing through screen shots how to bind database table to a JTable. I am using MySQL as that is what was requested. I wanted to highlight how easy it is to use NetBeans to do this task.  NetBeans takes care of all the persistence  part for you. In fact, I did not have to write any code at all for this.

Here are the steps:

Create a Java Application Project. From main menu File -> New Projects. Choose Java category, and  Java  Application.

 

Now delete the Main.java file under Source Packages. This file was automatically created by the wizard for you. Add a new JFrame Form . Right Click Project node -> New -> JFrame Form.

Drag and Drop JTable from the palette on to the designer, as shown below.

Now Drag and drop the database Table on to the JTable in the designer. To do this, go to the Services tab , and expand the Databases -> Your MySQL database node -> Table, and select the table you want to bind to the JTable. Drag and drop it on to the JTable on the designer.


Make sure the MySQL driver jar file is under the libraries node of the project. Build and Run the project. when running the project it will ask you to select the main class. This is because we deleted the main class earlier, and we now need to provide the main class to begin execution. Select the default as shown below.

Here's the screen shot on running the application. 


That's all there is to it. Not a line of code was written. If you browse in the projects window, you'll see the IDE automatically created the Person.java class and persistence.xml files.

Comments:

how can i configure the jtable for this example ?

Posted by joshua morotti on April 14, 2008 at 12:23 AM PDT #

I've tried the above, but I get a no entry symbol when I try to drag-and-drop a DB table onto a JTable. Perhaps there is some property of the table that I need to set?

Posted by David Etheridge on April 17, 2008 at 05:24 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2008 by radhika