Java Program To Insert Data Into MySQL Table Using JDBC

In this post, we are going to learn several things at the same time. You are being suggested to use NetBeans or Eclipse or any platform you prefer to work on java projects.
Though it is a simple program, still you have to use JDBC which does not reside in JDK.

Java Program To Fetch Data From MySQL Table (Database) Using JDBC

What is JDBC?

JDBC is Java Database Connectivity. Using JDBC we will be able to make the connection between the database and our java program.
In this program, we will connect MySQL Server with Java. So we need a connector for MySQL that is also known as
MySQL-connector-java and its available in the different versions
Here is the official link to find the file. Or you can download it yourself from google search.
after downloading the zip file you have to open it. there you will find a jar file MySQL-connector-java-version
you have to add this file to your project library. Just add this jar file to your project library

for NetBeans User just right click on your project name>>>>properties>>>>>Libraries>>>>>Add JAR/FOLDER>>>>choose the jar file

For eclipse user

Right click on your project name>>>Properties>>>>Java Build Path>>>>Add JARs>>>>choose your file here

Turn on your MySQL server and create a table as per your wish where you are going to insert data or records.
I have created a table like this

Mysql table

here is the create table statement

`username` varchar(20) NOT NULL,
`password` varchar(10) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`country` varchar(20) DEFAULT NULL,
`age` int(5) DEFAULT NULL,
`sex` varchar(6) DEFAULT NULL,

Here is the Java Source code to insert data/records into MySQL database

import java.sql.*;
import java.util.Scanner;

public class Code{
    public static void main(String[] args){
        Scanner s=new Scanner(;
         try {
             System.out.println("Enter username:");
             String username;
             System.out.println("Enter password:");
      String password;
      System.out.println("Enter full name:");
      String name;
      System.out.println("Enter email:");
      String email =;
      System.out.println("Enter country:");
      String country =;
      System.out.println("Enter age:");
      String age;
      System.out.println("Enter sex:");
      String sex;
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crud", "dbuser", "dbpassword");
   PreparedStatement pst = con.prepareStatement("insert into data(username,password,name,email,country,age,sex) values(?,?,?,?,?,?,?)");

      pst.setString(2, password);
       pst.setString(3, name);
        pst.setString(4, email);
        pst.setString(5, country);
        pst.setString(6, age);
        pst.setString(7, sex);
      int i = pst.executeUpdate();
        System.out.println("failed to add");
    catch (Exception e){

Output Will be like this :

Enter username:
Enter password:
Enter full name:
Enter email:
[email protected]
Enter country:
Enter age:
Enter sex:
BUILD SUCCESSFUL (total time: 32 seconds)

Now if you check your database by this statement

select * from data;

you will find your data has been inserted successfully.
Explanation of this program:

dbuser and db password is the password and username for my database.You have to put your database id and password here.



this is the driver class which is building the connection with MySQL server.


this is the database URL

3306 is the default port of MySQL server and crud is my database name which I have created, You may use your own.
and then the username and password of my database server. The default username for MySQL server is “root”

and password is ” “. that means blank password.


 PreparedStatement pst = con.prepareStatement("insert into data(username,password,name,email,country,age,sex) values(?,?,?,?,?,?,?)");

this is the PreparedStatement line.
in place of  “?” sign, it will take the String user will provide at the time of the execution of that query.

Hope this post will help the learners. for any query or problem please tell us in the comment section below so that we can get back to you.


Leave a Reply

Your email address will not be published. Required fields are marked *