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.

Ads

Tutorials   
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
Ads

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

Ads
Share on Google+Share on Google+

JDBC autocommit

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

Advertisements

 

Discuss: JDBC autocommit  

Post your Comment


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

Ads

 

Ads