Creating a Database Table

Database: A database is a large collection of data or information to stored in our computer in an arranged way.

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table Change Column Name of a Table Adding a New Column Name in Database Table Retrieving All Rows from a Database Table Inserting values in MySQL database table Retrieving Tables from a Database Deleting a Table from Database Description of Database Table Creating a MySQL Database Table to store Java Types Creating a Database Table Creating a Database in MySQL
Ads

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. 

JDBC Video Tutorial: Creating database table from Java Program

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();
    }
  }
}

Download this example.

Check the JDBC Programming Video tutorials.

                         

Advertisements

Share on Google+Share on Google+

Creating a Database Table

Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Creating a Database Table   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
Dan Van Atta
November 8, 2011
Example needs work

1) catch(SQLException s){ System.out.println("Table all ready exists!"); } Should be: System.err.println("Error creating table or table already exists.") 2) con.close(); NPE possible here, should be: if( con != null ) { con.close() }; 3) This statement object is never closed: Statement st = con.createStatement();
Jango Jane
April 20, 2012
Very nice

Nice program...
Gagan preet Singh
September 23, 2012
thankyou

thankyou sir
Shivam Saxena
September 27, 2012
database connectivity in core java

my project can't connect from table by access
tanvi
November 10, 2012
java table

i used the code of create table but can you please tell me the further procedure to run even i m not getting ...
raja
September 17, 2012
jdbc

under standing is

Ads

 

Ads