Home Tutorial Java Jdbc batch in jdbc

 
 

batch in jdbc
Posted on: June 1, 2010 at 12:00 AM
In Batch processing, many queries work together as batch,run query in batch in java jdbc.
JDBC BATCH PROCESSING   

In Batch processing, many queries work together as batch. The following program include collective set of SQL queries executed altogether to the database.".addBatch()" method is used to add query in "Statement" object and "executeBatch()" method is used to run all queries altogether.

sqlbatch.java

import java.sql.*;

public class sqlbatch {

    public static void main(String args[]) {

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

        String url = "jdbc:mysql://192.168.10.13:3306/";
        String db = "ankdb";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";

        try {
         Class.forName(driver);
         con = DriverManager.getConnection(url + db, user, pass);
            

            st = con.createStatement();

            st.addBatch("INSERT INTO cellular " +
                    "VALUES('3036','9560534071')");
            st.addBatch("INSERT INTO cellular " +
                    "VALUES('3037','9560534072')");
            st.addBatch("INSERT INTO cellular " +
                    "VALUES('3038','9560534073')");
            

             st.executeBatch();

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

            System.out.println("ID  \tMOBILE");
            while (rs.next()) {
                System.out.print(rs.getInt(1) + "   \t");
                System.out.println(rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

output

Download this Code

Related Tags for batch in jdbc:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.