Books Related to Java Technology

Sunday, January 4, 2009

MySQL Create Database

  • In this example we are creating a databse table for establishing the connection with MySQL database by using the JDBC driver. A database is a large collection of data or information stored in our computer in a specified manner. It helps us for accessing, managing and updating the data easily. 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.

The following example Shows to you how to create a database in MYSQL:


import java.io.*;
import java.sql.*;

public class CreateDatabase{
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbname = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection (url+dbname,user,pass);
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("Dtabase Created Successfully");
} catch (SQLException s){ System.out.println("SQL Exception!");
}
} catch (Exception e){ e.printStackTrace();
}
}
}

No comments:

Post a Comment