Saturday, 26 July 2014

JDBC Create Database Demo

Enjoying Java & Database Tutorials?

Subscribe to Ram N Java on YouTube for beginner-friendly coding lessons and demos!

🔔 Subscribe to Channel

How to Create a Database Using JDBC in Java

When developing Java applications that manage relational data, you often need to set up new database instances programmatically. Using JDBC (Java Database Connectivity), you can send standard SQL DDL (Data Definition Language) commands straight from your Java code to create a brand new database on your server.

The SQL Create Database Statement

In SQL, initializing a new database requires a single instruction:

CREATE DATABASE students;

This query tells the database engine to allocate storage and register a new catalog named students.

Step-by-Step Java Implementation

Here is the standard workflow to create a database dynamically using JDBC:

  1. Register the JDBC Driver: Load your database vendor driver class into memory using Class.forName().
  2. Connect at Server Level: Connect to the database server host without referencing a specific database name in the connection URL string.
  3. Create a Statement: Instantiate a Statement or PreparedStatement to hold your DDL query.
  4. Execute the Statement: Invoke executeUpdate() to create the database on the server.
  5. Close Resources: Ensure both connection and statement objects are properly closed in a finally block or via try-with-resources.

Watch More JDBC Tutorials


Click here to watch in Youtube : https://www.youtube.com/watch?v=jI1Ja0IhBUU

Click the below Image to Enlarge

JDBCCreateDatabaseDemo Project Dir Structure
JDBC Create Database Demo
JDBC Create Database Demo
JDBCCreateDatabase.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCCreateDatabase
{
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/";

    // Database credentials
    static final String USERNAME = "root";
    static final String PASSWORD = "root";

    public static void main(String[] args)
    {

        JDBCCreateDatabase jdbcCreateDatabase = new JDBCCreateDatabase();
        jdbcCreateDatabase.createDatabase();

    }

    private void createDatabase()
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try
        {
            /*
             * Register the JDBC driver in DriverManager
             */

            Class.forName(JDBC_DRIVER);

            /*
             * Establish connection to the Database using DriverManager
             */

            connection = DriverManager
                    .getConnection(DB_URL, USERNAME, PASSWORD);

            String sql = "CREATE DATABASE STUDENTS";

            /*
             * Execute the query
             */
            preparedStatement = connection.prepareStatement(sql);

            int returnValue = preparedStatement.executeUpdate();

            System.out.println("returnValue : " + returnValue);

        }
        catch (SQLException se)
        {
            /*
             * Handle errors for JDBC
             */
            se.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            /*
             * Handle errors for Class.forName
             */
            e.printStackTrace();
        }
        finally
        {
            /*
             * finally block used to close resources
             */
            try
            {
                if (preparedStatement != null)
                {
                    preparedStatement.close();
                }
            }
            catch (SQLException sqlException)
            {
                sqlException.printStackTrace();
            }
            try
            {
                if (connection != null)
                {
                    connection.close();
                }
            }
            catch (SQLException sqlException)
            {
                sqlException.printStackTrace();
            }
        }

    }
}

Output
returnValue : 1

Environment Used 

JDK version : 1.7.0_51
Mysql Server version : 5.6.19 

To Download JDBCCreateDatabaseDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/jdbc/JDBCCreateDatabaseDemoApp.zip?attredirects=0&d=1

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Tutorials