RECORD INSERTION IN MYSQL TABLE

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.

https://milindjagre.wordpress.com/2014/04/25/mysql-database-connection-through-java/

 

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(System.in);

 

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(scanner.next());

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

name = scanner.next();

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

age = Integer.parseInt(scanner.next());

scanner.close();

 

try {

try {

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”).newInstance();

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

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

} catch (ClassNotFoundException e) {

e.printStackTrace();

return;

}

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) {

e.printStackTrace();

return;

}

connection.close();

System.out

.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
recordinsertionoutput

 

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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s