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();
}
}
}
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
Creating a Database Table
Database: A database is a large collection of data or information to stored in our computer in an arranged way. It helps us for accessing, managing and updating the data easily. In this example we are using MySQL database, which is a RDBMS. A RDBMS (Relational Database Management System) is a type of DBMS (Database Management System) which stores the data in the form of tables. RDBMS is very powerful as it doesn't need to aware how the data is related or how it is going to be extracted from the database. So, we can view the same database in many different ways.
Table: A table is basic component of database (DB) that has number of rows and columns. All tables are stored in a specific database.
Here we are providing you an example with code and it's description that helps you to create a database table by using java file. Brief description given below:
Description of program:
Firstly in this program we are going to establish the connection with database and creating a table with some fields. If table name already exists then we are displaying the message "Table already exists!".
Description of code:
Statement:
It is a interface. Statement object executes the SQL statement and returns the result it produces.
createStatement():
It is a method of Connection interface. which returns Statement object. This method will compile again and again whenever the program runs.
CREATE TABLE table_name(field_name):
An appropriate code used for creating a table with given field name.
executeUpdate(String table):
This method also executes SQL statement that may be INSERT, UPDATE OR DELETE statement are used in the code. It takes string types parameters for SQL statement. It returns int.
Here is the code of program:
import java.sql.*;
public class CreateTable{
public static void main(String[] args) {
System.out.println("Table Creation Example!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try{
Class.forName(driverName).newInstance();
con = DriverManager.getConnection(url+dbName, userName, password);
try{
Statement st = con.createStatement();
String table = "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))";
st.executeUpdate(table);
System.out.println("Table creation process successfully!");
}
catch(SQLException s){
System.out.println("Table all ready exists!");
}
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
Creating a MySQL Database Table to store Java Types
Dear user, consider a case where we need to store a java types in our database table. Now one question may arise in your mind that whether the MySQL supports java types or not. This section describes how to create a table in MySQL database that stores all java types. Here we are providing you an example with code for creating a table to store java types. Brief description is given below:
Description of program:
In this program we are going to establish the connection between MySQL database table and java file. After the connection has been established creates a database table for storing all java types. We have used most of the java types provided to us by the jdbc.
Here is the code of program:
import java.sql.*;
public class CreateMySqlTable{
public static void main(String[] args) {
System.out.println("Creating a Mysql Table to Store Java Types!");
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db, user, pass);
try{
Statement st = con.createStatement();
<<<<<<< CreateMySqlTable.shtml String table =
"CREATE TABLE java_DataTypes2
("+ "typ_boolean BOOL, "
======= String table
= "CREATE TABLE java_DataTypes2("+ "typ_boolean BOOL, "
>>>>>>> 1.7 + "typ_byte TINYINT, "
+ "typ_short SMALLINT, "
+ "typ_int INTEGER, "
+ "typ_long BIGINT, "
+ "typ_float FLOAT, "
+ "typ_double DOUBLE PRECISION, "
+ "typ_bigdecimal DECIMAL(13,0), "
+ "typ_string VARCHAR(254), "
+ "typ_date DATE, "
+ "typ_time TIME, "
+ "typ_timestamp TIMESTAMP, "
+ "typ_asciistream TEXT, "
+ "typ_binarystream LONGBLOB, "
+ "typ_blob BLOB)";
st.executeUpdate(table);
System.out.println(table);
con.close();
}
catch (SQLException s){
System.out.println
("Table is all ready exists!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
No comments:
Post a Comment