We are going to insert the data into mysql table ‘sample’.

We will use Java programming language for doing the same.


In case you missed how to connect your database with the JAVA programming language, you should refer this link below.


Once you establish the connection with MySQL database, all you need to do is to select a table and insert desired data into it with the help of executeUpdate(query) method of Statement object.

The executeUpdate(query) returns number of rows which got updated in the process. After inserting one record into the table, if this method returns 1, then we can say that the record got inserted successfully.

Rest of the things are same as the previous post, the only thing which is different has been discussed above. So please have a careful look at the program below.


above. So please have a careful look at the program below.

package mysqlandjava;import java.sql.Connection;import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Scanner;


public class InsertingDataIntoMySQLTable {


public static void main(String[] args) throws ClassNotFoundException,

SQLException {

// TODO Auto-generated method stub

Scanner scanner = new Scanner(;


String tableName = “sample”;

String fieldName1 = “id”, fieldName2 = “name”, fieldName3 = “age”;

int id = 0;

String name = null;

int age = 23;

System.out.println(“enter id”);

id = Integer.parseInt(;

System.out.println(“enter name”);

name =;

System.out.println(“enter age”);

age = Integer.parseInt(;



try {

try {


} catch (InstantiationException e) {

// TODO Auto-generated catch block


} catch (IllegalAccessException e) {

// TODO Auto-generated catch block



System.out.println(“Driver Name accepted…”);

} catch (ClassNotFoundException e) {




Connection connection = null;


try {


connection = DriverManager.getConnection(“jdbc:odbc:mysqldsn”,

“root”, “root”);

System.out.println(“connection established…”);


String insertQuery = “insert into  ” + tableName + ” (”

+ fieldName1 + “, ” + fieldName2 + “, ” + fieldName3

+ “) values (” + id + “, ‘” + name + “‘, ” + age + ” );”;

Statement statement = connection.createStatement();

int rowsUpdated = statement.executeUpdate(insertQuery);


System.out.println(rowsUpdated + ” row(s) updated successfully…”);


} catch (SQLException e) {






.println(“connection closed…\nCongrats your task is done completely”);





The output screenshots are shared below.

1)      After executing the program, the command line looks like this

output of record insertion in MySQL table


2)      And the MySQL command line changes from Empty Set to 1 Row in Set.

table contents
changed contents of the table


Thank you for having a read and suggestions are always welcome.

Published by milindjagre

I founded my blog four years ago and am currently working as a Data Scientist Analyst at the Ford Motor Company. I graduated from the University of Connecticut pursuing Master of Science in Business Analytics and Project Management. I am working hard and learning a lot of new things in the field of Data Science. I am a strong believer of constant and directional efforts keeping the teamwork at the highest priority. Please reach out to me at for further information. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: