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. https://dev.mysql.com/downloads/connector/j/
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.
example:
I have created a table like this

Mysql table
here is the create table statement
CREATE TABLE `data` ( `id` int(10) NOT NULL AUTO_INCREMENT, `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, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1
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(System.in); try { System.out.println("Enter username:"); String username =s.next(); System.out.println("Enter password:"); String password =s.next(); System.out.println("Enter full name:"); String name =s.next(); System.out.println("Enter email:"); String email = s.next(); System.out.println("Enter country:"); String country = s.next(); System.out.println("Enter age:"); String age =s.next(); System.out.println("Enter sex:"); String sex =s.next(); Class.forName("com.mysql.jdbc.Driver"); 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(1,username); 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(); if(i!=0){ System.out.println("added"); } else{ System.out.println("failed to add"); } } catch (Exception e){ System.out.println(e); } } }
Output Will be like this :
run: Enter username: ironman Enter password: iron Enter full name: marvel Enter email: iron@iron.com Enter country: india Enter age: 35 Enter sex: male added 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.
Class.forName("com.mysql.jdbc.Driver");
this is the driver class which is building the connection with MySQL server.
jdbc:mysql://localhost:3306/crud
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