Home Jdbc Jdbc-mysql Creating a Database in MySQL
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Creating a Database in MySQL

Advertisement
After establishing the connection with MySQL database by using the JDBC driver, you will learn how we can create our database.

Creating a Database in MySQL

     

After establishing the connection with MySQL database by using the JDBC driver, 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 Management System) 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:

CREATE  DATABASE  db_name;  
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.io.*;
import java.sql.*;

public class CreateDatabase{
  public static void main(String[] args) {
  System.out.println("Database creation example!");
  Connection con = null;
  try{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection
(
"jdbc:mysql://localhost:3306/jdbctutorial","root","root");
  try{
  Statement st = con.createStatement();
  BufferedReader bf = new BufferedReader
(
new InputStreamReader(System.in));
  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){
  e.printStackTrace();
  }
  }
}

Download this example.

Output of program:

C:\vinod\jdbc\jdbc\jdbc-mysql>javac CreateDatabase.java

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

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


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: April 13, 2007

Related Tutorials

Ask Questions?    Discuss: Creating a Database in MySQL   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Visuwa Bharathi.K
August 22, 2011
create table

Can u explain how to create table in JDBC.?
Aru
November 16, 2011
v nice and basic tutorial

this tutorial was very nice, brief and to the point
Sudeep
June 4, 2012
Creation of database.

In the above program there is a small change that needs to be done for the database creation: con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/","root","root"); Database name must not be specified in the above command.
Anie
July 10, 2012
where

Hi, after creating the database, where can u find the sql dump for the database created?
Andre
July 26, 2012
Files and directories zipping issues

U r good.can i ask u for ur help on sometin.can u help me with a code on how to zip both files and directories and save them into a folder that will be specified by a user.i'm creating one and it's not zipping them and saves them outside the specified folder.would have loved to send you the codes i wrote for this so that you can help with any corrections but this aint youe email. Hope i'm not asking too much n please could you send your reply to d mail written above?. Thanks
SAMEER CHIKATE
October 1, 2012
database connectivity

how to connect database......... what is the code for connectivity................ can i connect more than 1 table with my database
DMCA.com