Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc JDBC autocommit

Related Tutorials


 
 

Share on Google+Share on Google+

JDBC autocommit

Advertisement
Jdbc connection is said to be auto commit if all the statement in SQL will be committed and executed altogether as a individual transaction.

JDBC autocommit

     

JDBC connection is said to be auto commit if all the statement in SQL will be committed and executed altogether as a individual transaction.

Understand with Example

In this Tutorial we want to describe you a code that helps in understanding JDBC auto commit. The code illustrates the list of following steps-

  1. The first step is need to import a package java.sql.*;This package defined the list of all classes in java.sql.*.
  2. database driver  - The second step is the loading of driver class by calling Class.forName( ) passed with the Driver class name as an argument. As  driver is loaded ,you connect the front end of the Java application to the backend database. In case there is an exception occurred  in loading a database driver  in the try block. The catch block check the exception and println print the exception.
  3. con = DriverManager.getConnection( ) - This method attempt to make a database connection to a URL
  4. con.autocommit(false)  - This is used to disable auto commit.
  5. st = con.createconnection( ) -This method return you an Sql object. Once a connection is made ,your application can interact with backend database. A connection object is used to send and execute SQL Statement to a backend database.
  6. rs = st.executequery(sql)   -  This is used  for select query in sql and the value obtained from it stored in  result set rs.The Result Set enables you to access a table containing data. The table row contain a retrieved element from a database in a sequence.
  7. next( ) -  The next method is used to retrieve a element successively  through the rows obtained from a result set.

JdbcAutocommit .java

import java.sql.*;
public class JdbcAutocommit {
    public static void main(String args[]) {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            con.setAutoCommit(false);// Disables auto-commit.
            st = con.createStatement();
            String sql = "select * from customer";
            rs = st.executeQuery(sql);
            System.out.println("First Name  \tLast Name");
            while (rs.next()) {
                System.out.print(rs.getString("First_Name") + "   \t");
                System.out.println(rs.getString("Last_Name"));
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output

First Name  	Last Name
girish   	tewari
mahendra   	singh
komal   	singh
yogesh   	pandey
pinku   	tripathi
deepak   	tripathi

Download code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 17, 2008

Related Tutorials

Discuss: JDBC autocommit  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com