JDBC Insert Record

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

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 (rs.next()) {
                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??