Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: c database orm table data form io sed column remove unique key field columns this tab move row for rows

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

the o/p of the above prog. is:-
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:262)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:322)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:130)
at CreateDatabase.main(CreateDatabase.java:9)
why???????

Posted by piyush on Tuesday, 09.9.08 @ 14:58pm | #78249

jdbc programs are very will but small exampls r requierd

Posted by srilatha on Friday, 03.7.08 @ 11:52am | #51812

Refering this site we can improve basic knowledge of DataBases.

Posted by Gayan on Friday, 06.15.07 @ 16:34pm | #19311

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.