Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JDBC Insert Record

In this Tutorial we want to explain you an example from JDBC Insert Record.


JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout

JDBC Insert Record


The Tutorial wants to explain a code that describe a JDBC Insert Record. In this code we have a class JdbcInsertRecord.Inside this class we load a driver by calling class.forName( ) that accept driver class as argument. The Driver Manager calls getConnection ( ),which returns a connection object. This help in built a connection between url and database. In JDBC,Statement object perform an insert record using  batch updates. The createStatement ( ) return you a statement object, that is used to send and execute sql statements in the database. We can send multiple sql statement to an empty array and execute them as a batch. The Auto commit can be disable by using setAutoCommit (false), while trying to updates the batches. The setAutoCommit ( false) provides you a rollback of the entire transaction, Incase any SQL statement fail during the execution of batch update. The three INSERT statements are added to the batch, using the addBatch ( ) method. The executeBatch ( ) return you the update counts of arrays, this means the total number of rows affected when sql statement is processed. Finally the execute Query ( ) return you the record set from a table.

   next ( ) - This method is used to return you the next series element in the table.

Finally the println print the required Insert record out put as specified. Incase the exception exists in try block, the subsequent catch block caught and handle the exception.

import java.sql.*;

public class JdbcInsertRecord {
        public static void main(String args[]) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/komal" , "root", "root");
            con.setAutoCommit(false);// Disables auto-commit.

            st = con.createStatement();

            st.addBatch("INSERT INTO stu VALUES('1','Komal')");
            st.addBatch("INSERT INTO stu VALUES('2','Ajay')");
            st.addBatch("INSERT INTO stu VALUES('3','Santosh')");


            String sql = "select * from stu";
            rs = st.executeQuery(sql);

            System.out.println("No  \tName");
            while ( {
                System.out.print(rs.getString(1) + "   \t");
        } catch (Exception e) {


No  	Name
1   	Komal
2   	Ajay
3   	Santosh

Download code


Share on Google+Share on Google+

JDBC Insert Record

Posted on: November 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JDBC Insert Record  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 5, 2011
Insert in databse using gui TextField

String t1=txt1.getText(); String t2=txt2.getText(); s.execute("insert into hello values(t1,t2)"); I want to store the record in database through TextField,But there is the problem "insert into hello values(t1,t2)". please help me to solve this.
October 16, 2011

this code works fine but wat if i wish to save all d data to b inserted in a table get saved in different objects...? suppose in the above example if i write int id=1; String nm="komal"; but this doesnt work...wat shoulb the solution??