JDBC batch

JDBC is simply a Java Database Connectivity. This Connectivity provides you an API, that provides you to interact your Java Application in your front end with the backend.

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 batch

     

JDBC is simply a Java Database Connectivity. This Connectivity provides you an API, that provides you to interact your Java Application in your front end with the backend. The backend can be SQL-2000,2005,MySql. A batch in JDBC is a set of sql statement  altogether to the database for execution.

Understand with Example

We have already know with the JDBC Connection in previous tutorial. In this Tutorial we want to describe you a code that helps you in understanding a JDBC batch. The code explain you a JDBC,that include collective set of SQL queries executed altogether to the database. For this we define a class JdbcBatch, Inside the main method, we define a url,database,driver name,username and password that helps you to establish a connection an application with the backend database. The list of method helps you in execution of JDBC batch are as given below - 

 Before defining a class, you need to import a java.sql.* :The package import all defined classes, which are required for providing network interface between the application and database.

 Driver loading  : load the driver class by calling a Class.forName ( ) with the driver passed as an argument. Driver Connection provides you to connect  the front end in java application and backend in sql.

con = DriverManager.getConnection(url+db,user,pass)  -The.getConnection ( ) return you an object of connection class.This method attempt to connect a database connection to the specified url.

The create Statement ( ) method return you an object of statement. An statement is used to execute a sql statement in the backend

add Batches( ) :  An object of statement  call a add Batches ( ) method, that set the batch values into specified database.

st.executeBatches ( ) :This method is used to execute the sql Batches.

 execute Query ( ) -  This method return you an object of result set. This object include set of all the values from the table "stu". The next ( ) return you the successive element from a table of the database.Finally the println print the element stored in the database.

JdbcBatch .java

import java.sql.*;
public class JdbcBatch {
    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();
            st.addBatch("INSERT INTO stu " +
                    "VALUES('1','name2')");
            st.addBatch("INSERT INTO stu " +
                    "VALUES('2','name3')");
            st.addBatch("INSERT INTO stu " +
                    "VALUES('3','name4')");
            
             st.executeBatch();
            String sql = "select * from stu ";
            rs = st.executeQuery(sql);
            System.out.println("No  \tName");
            while (rs.next()) {
                System.out.print(rs.getString(1) + "   \t");
                System.out.println(rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output

No  	Name
4   	Komal
5   	Ajay
6   	Santosh 

Download code

Advertisements

Share on Google+Share on Google+

JDBC batch

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

Advertisements

Ads
 

Discuss: JDBC batch  

Post your Comment


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

Ads

 

Ads