Java Execute Multiple SQL Statements

Chapter: JDBC Last Updated: 10-09-2016 07:32:14 UTC

Program:

            /* ............... START ............... */
                
import java.sql.*;

public class JavaExecuteMultipleSQLStatments {

	public static void main(String[] args) throws Exception {
		Class.forName("org.apache.derby.jdbc.ClientDriver");
		Connection con = DriverManager.
				getConnection("jdbc:derby://localhost:1527/testDb", "name", "pass");
		Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
		String insertEmp1 = "insert into emp values (10,'Jhon','trainee')";
		String insertEmp2 = "insert into emp values (11,'jayes','trainee')";
		String insertEmp3 = "insert into emp values (12,'shine','trainee')";
		con.setAutoCommit(false);
		stmt.addBatch(insertEmp1);
		stmt.addBatch(insertEmp2);
		stmt.addBatch(insertEmp3);
		ResultSet rs = stmt.executeQuery("select * from emp");
		rs.last();
		System.out.println("rows before batch execution= " + rs.getRow());
		stmt.executeBatch();
		con.commit();
		System.out.println("Batch executed");
		rs = stmt.executeQuery("select * from emp");
		rs.last();
		System.out.println("rows after batch execution= " + rs.getRow());
	}
}

                /* ............... END ............... */
        

Tags

Execute Multiple SQL Statements, Java, JDBC

Similar Programs Chapter Last Updated
Java JDBC Savepoint And Rollback JDBC 10-09-2016
Java JDBC Commit Statement Example JDBC 10-09-2016
Java JDBC Sort Table Contents JDBC 10-09-2016
Java JDBC Search Table Contents JDBC 10-09-2016
Java JDBC Update Table JDBC 10-09-2016
Java Retrieve Table Contents JDBC 10-09-2016
Java JDBC Table Edit Example JDBC 10-09-2016
Java Connect To Database Example JDBC 10-09-2016
JDBC SQL Cursors Using CallableStatement JDBC 26-06-2016
JDBC Batch Execution In Java Using CallableStatement JDBC 26-06-2016
JDBC Stored Procedure Returns OUT Parameters In Java JDBC 23-06-2016
JDBC Stored Procedure Execution In Java JDBC 23-06-2016
JDBC Prepared Statement With ResultSet In Java Example JDBC 18-06-2016
JDBC Prepared Statement In Java Example JDBC 18-06-2016
JDBC Update Query In Java Example JDBC 18-06-2016
JDBC Query Execution In Java Example JDBC 18-06-2016
JDBC Query In Java Example JDBC 18-06-2016
Java JDBC Connection Example JDBC 18-06-2016

1