Monday, May 24, 2010

Creating a Database in MySQL

After establishing the connection withMySQL database by using the JDBCdriver, you will learn how we can create our database. A database is a large collection of data or information stored in our computer in an arranged way. It helps us for accessing, managing and updating the data easily. In this example we are going to create a database by MySQL and with the help of some java methods and SQL statement. A RDBMS(Relational Database Management System) is a type of DBMS (Database ManagementSystem) which stores the data in the form of tables. So, we can view and use the same database in many different ways.

Description of program:

Firstly this program establishes the connection with MySQL database and takes a database name as its input in the database query and only after that it will create a new database and show a message "1 row(s) affected" otherwise, it displays "SQL statement is not executed!".

Description of code:

Above code is used for creating a new database. It takes a database name and then a new database is created by that name.

Here is the code of program:

import java.sql.*;

public class CreateDatabase{
public static void main(String[] args) {
System.out.println("Database creation example!");
Connection con = null;
con = DriverManager.getConnection
Statement st = con.createStatement();
BufferedReader bf = new BufferedReader
new InputStreamReader(;
System.out.println("Enter Database name:");
String database = bf.readLine();
st.executeUpdate("CREATE DATABASE "+database);
System.out.println("1 row(s) affacted");
catch (SQLException s){
System.out.println("SQL statement is not executed!");
catch (Exception e){

Download this example.

Output of program:


C:\vinod\jdbc\jdbc\jdbc-mysql>java CreateDatabase
Database creation example!
Enter Database name:
1 row(s) affacted

No comments:

Post a Comment