Lesson 3: Inserting Data From Java to MySQL Database

I will assume that you already read the the previous post

Lesson 1: Connecting MySQL Database to JAVA using NetBeans IDE

Lesson 2: Getting Data From MySQL Database using JAVA

Lesson 3: Inserting Data From Java to MySQL Database

1 –  Create a new JFrame form name it as insert and add the following input field with the following variable name

Label – input variable name

username – txt_username

password – txt_password

First Name – txt_fname

Last Name – txt_lname

Age – txt_age

Gender – txt_gender

Note: Make sure your table have this columns

2 – Now declare the following global variables

As you notice we didn’t use the “PreparedStatement” because prepared statement is only for fetching of data and for updates of data we will use Statement”

3 – Now add the database connection to your windowOpened See Lesson 2: #6

The primary setup is complete! Lets now create a basic query

4 – Insert the below code to your Register Button

Run your application. Click the register button if it is correct it will show a “Success” Dialog and it will insert a new data to your database

On the above tutorial we insert a static data. Now we will use all the input fields that we setup on the first stage

Run your application!