Binding JTable with MySQL table
By radhika on Mar 21, 2008
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:
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.