Home Tutorial Java Jdbc Jdbcbatch JDBC Batch Example With SQL Select Statement

 
 

Share on Google+Share on Google+
JDBC Batch Example With SQL Select Statement
Posted on: October 15, 2010 at 12:00 AM
Advertisement
In this example, we are discuss about SQL select statement with JDBC batch process.

JDBC Batch Example With SQL Select Statement:

In this example, we are discuss about SQL select statement with JDBC batch process. We will create SQL select  statement and execute it in a result set and after that fetch this result set and display data on the client screen.

In this example we will used two database tables "user" and "address". First we will insert value in these table and then fetch data by using select statement.

The following code snippet show the process is:

import java.sql.*;
public class BatchSelect {  
  static final String driver = "com.mysql.jdbc.Driver";  
  static final String url = "jdbc:mysql://localhost:3306/";
  static final String dbname = "roseindia_jdbc_tutorials";
    static final String dbuser = "root";
    static final String dbpassword = "root";
  public static void main(String[] args) {
  Connection connection = null;
  Statement stmt = null;
  try{
     Class.forName(driver);
     connection = DriverManager.getConnection(url+dbname,dbuser,dbpassword);
     try{
    stmt = connection.createStatement();          
    connection.setAutoCommit(false);          
    String insertquery1 = "INSERT INTO user (user_id, user_name) VALUES(1,'Brijesh')";
    stmt.addBatch(insertquery1);          
    String insertquery2 = "INSERT INTO address (id, address) VALUES(1,'Delhi')";
    stmt.addBatch(insertquery2);        
    stmt.executeBatch();
    SelectUserdata(stmt);
    SelectAddressdata(stmt);        
    connection.commit()
    }
    catch (SQLException s){
    System.out.println("SQL Exception " + s);
    }
   }
   catch (Exception e){
     e.printStackTrace();
   }  
  }
 public static void SelectUserdata(Statement stmtthrows SQLException{
       String sql = "SELECT user_id, user_name FROM user";
       ResultSet rs = stmt.executeQuery(sql);
       while(rs.next()){
          int user_id  = rs.getInt("user_id");
          String user_name = rs.getString("user_name");

          System.out.print("user_id: " + user_id);
          System.out.println(" , user_name: " + user_name);       
       }
      rs.close();  
    }
  public static void SelectAddressdata(Statement stmtthrows SQLException{
              
       String sql = "SELECT id, address FROM address";
       ResultSet rs = stmt.executeQuery(sql);
       while(rs.next()){
          int address_id  = rs.getInt("id");
          String address = rs.getString("address");
          
          System.out.print("address_id: " + address_id);
          System.out.println(" , address: " + address);       
       }
      rs.close();
    }
   }

Now we will run this example with the help of eclipse IDE and see the output.

Program output:

The eclipse console output is:

The database table output are:

Program source code

Advertisement

Related Tags for JDBC Batch Example With SQL Select Statement:


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: October 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com