This tutorial will teach you how to load your SQLite table values to Java Swing JTable using NetBeans IDE. Before following the tutorial, you must have an SQLite table with at least 1 row.

First, we need to create a new project. Click “File” from the menu bar on top. Choose “New Project” or alternatively you can open New Project by pressing and holding the keys Ctrl+Shift+N. This will open the New Project dialogue box.

Java New Project Dialogue

Pay no attention to the theme because I’m using a different theme. Once you’re in the “New Project” dialogue box, choose “Java” from the Categories on the left side and under “Projects” choose “Java Application” and click “Next >”.
This will take you to the “New Java Application” dialogue box. Fill the fields like so:

This dialogue box is where we can set the name of our project, its location, and Main Class creation. For the meantime, leave everything as it is except for the project name and the “Create Main Class” checkbox.
Let’s name our project “JTable using rs2xml” and un-tick the “Create Main Class” checkbox then click “Finish”.

The dialogue box will close and on the left side of NetBeans, under “Projects”, you’ll see that our project has been created.

Java NEw Project Shot

It is not recommended to start your project with a default package so right click on the “Sources Package”, hover to New and click “Java Package…” and name it “main”.

Next is we have to add the rs2xml and SQLite Jar files. Under Projects, right click on Libraries and click “Add Jar/Folder”. Navigate to where you saved the Jar file and then click “Open” from the dialogue box.

Download rs2xml Jar File.

Download SQLite Jar File.

Java Add Jar Files

Now we’ll add a new JFrame form. JFrame form is what will contain all of our controls. To add one, right click under Projects, hover to New then click “New JFrame Form”. The dialogue box below will show. Let’s rename the frame with “JTableTutu” by typing it into the Class Name field then click “Finish” below.

Java New Jframe form dialogue

On the right-hand side of your NetBeans Window, you’ll see the “Palette”.

Java Swing Pallette

Let’s first add the JTable. We can add Jtable to our JFrame by clicking “Table” from the Palette under “Swing Controls”. Hover back to the JFrame form, drop the JTable inside it. You can resize it to any size you want even the JFrame form itself.
In the same manner, we’ll add a JButton. Click Button from the Palette and add it on top of your JTable. You should have a similar setup like below by now. The image below shows the JButton has a text JButton2, yours may be JButton1.

Java JButton and JTable

Now we will add the codes. Currently, we are on the “Design” Page. To add the codes, we’ll have to navigate to “Source” page. There is more than 1 way to navigate to it but for now, we’ll stick with the conventional way. Click “Source” just beside “Design” located just above your JFrame form.

Java Swing Source Navigate

Just under the package declaration, paste the necessary files to be imported just like image below.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.proteanit.sql.DbUtils;

The light bulbs below it say all the imports are unused. This is normal because we haven’t added the codes yet that will use them. To start using them, paste the codes below under the class declaration.
Connection conn;
ResultSet rs;
PreparedStatement pstmt;

Java Conn declaration
Java Swing Connection Calling

Next is load the database values to the JTable. Navigate back to “Design” and double click on the JButton and this will load actionPerformed event on your JButton. Paste the codes below.
try{
pstmt = conn.prepareStatement(“Select * from test”);
rs = pstmt.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}catch(SQLException e){
e.getMessage();
}
Note that this particular line of codes: (“Select * from test”); is getting the “test” table from my SQLite table. You may have a different table name. To call your table value, just replace the “test” with the name of your table.

Java Swing method

Now run your project by pressing F6 or by clicking the “Run” button.

Java Swing Run

If it asked you to choose a Main Class, just choose the name of your class.
When your project loads, click the JButton and your database values should now be displayed on your JTable.

Java Swing Sample Result

That’s how you add SQLite table values on a Java Swing Jtable. Browse WhutTheTech for more Java tips and tech articles.

Leave a Reply

Close Menu