Lesson 2: Getting Data From MySQL Database using JAVA

  • Blog, Others

Lesson 2: Getting Data From MySQL Database using JAVA

Read first the previous lesson “Lesson 1: Connecting MySQL database to JAVA using NetBeans IDE“.

To continue lets now create a new JFrame form. We will use this to show the Database data

Just follow the steps below.

1 – Right click Source Package > New > JFrame Form

2 – Lets name our new JFrame From as “Dashboard” and click Finish

This will show the NetBeans JFrame tools.

3 – Now Right click the JFrame Panel Events > Window > WindowOpened

This will open the source code panel

4 – Now lets import java.sql.*

5 – After importing java.sql.* lets now declare this 3 global variables

6 – Now under the windowOpened See step #3 add the code below.

Now lets test our Application by Running it. Go to RunRun Files or Click [Shift + F6]. If no error then everything is working fine.

Render data from database to JTable

You need to back to the Design Panel

7 – Select the JTable from Palette by dragging it to our panel

8 – Right click the JTable. This will open the  Customizer Dialog Go to Columns tab and change the following column base to your database column: For this tutorial I will going to add “First Name, Last Name and Age”

For the Rows tab delete the empty rows to make it empty rows

After that click close

9 – Now lets change the variable name for our JTable by right clicking it and select “Change Variable Name” rename it as “table”

10 – Now lets render the data Go to JPanel windowOpened see step#3  and add the following code below conn variable

Now run your application [Shift + F6]

Note: Make sure you have a dummy data on your database table.