In this example, we are discuss about database creation using JDBC Batch process on the database server.
First of all, we will create a java class BatchDatabaseCreation.java. In this class we will create database connection as:
Connection connection =
null;
Class.forName(
"com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/","root","root");
Now we will used sequence of the step for Batch processing for alter database table.
1. Create Statement object using createStatement() method on the created Connection object as:
Statement stmt = connection.createStatement();
2. set auto commit is false using setAutoCommit() as:
connection.setAutoCommit(
false);
3. Create MySql create database statement for create new database and add these statement in batch using addBatch() method as:
String dbName =
"roseindia_jdbc_tutorials";
String createdatabasequery =
"Create Database "+dbName;
stmt.addBatch(createdatabasequery);
4. Execute the MySql statements using executeBatch() method on created statement object as:
stmt.executeBatch();
5. Last commit the connection using commit() method as:
connection.commit();
The code of the BatchDatabaseCreation.java class is:
|
import java.sql.*; public class BatchDatabaseCreation { public static void main(String[] args) { try{ Connection connection = null; Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/","root","root"); try{ // Create statement object Statement stmt = connection.createStatement(); // Set auto-commit to false connection.setAutoCommit(false); String dbName = "roseindia_jdbc_tutorials"; // create database query String createdatabasequery = "Create Database "+dbName; stmt.addBatch(createdatabasequery); stmt.executeBatch(); System.out.println("Database Creation Sucessfully."); // connection committed connection.commit(); } catch (SQLException s){ System.out.println("SQL Exception " + s); } } catch (Exception e){ e.printStackTrace(); } } } |
Now we will run this example and see the output.
Program output:
The eclipse console output is:

The database is:
