Creating a Database Table

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

Creating a Database Table

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.

                         

Tutorials

  1. Mapping MySQL Data Types in Java
  2. Connecting to a MySQL Database in Java
  3. Creating a Database in MySQL
  4. Creating a Database Table
  5. Creating a MySQL Database Table to store Java Types
  6. Description of Database Table
  7. Deleting a Table from Database
  8. Retrieving Tables from a Database
  9. Inserting values in MySQL database table
  10. Retrieving All Rows from a Database Table
  11. Adding a New Column Name in Database Table
  12. Change Column Name of a Table
  13. Make Unique Column in Database Table
  14. Remove Unique Column in Database Table
  15. Arrange a Column of Database Table
  16. Arrange a Column of Database Table
  17. Sum of Column in a Database Table
  18. Deleting All Rows from a Database Table
  19. Delete a Specific Row from a Database Table
  20. Delete a Column from a Database Table
  21. Join tables in the specific database
  22. Join tables with the NATURAL LEFT JOIN operation
  23. Join tables with the NATURAL RIGHT JOIN operation
  24. Cross Join Tables in a Specific Database
  25. Prepared Statement Set Object
  26. Statement Batch Update
  27. Prepared Statement With Batch Update
  28. Select Records Using Prepared Statement
  29. Update Records using Prepared Statement
  30. Inserting Records using the Prepared Statement
  31. Count Records using the Prepared Statement
  32. Deleting Records using the Prepared Statement
  33. Using the Prepared Statement Twice
  34. Set Data Types by using Prepared Statement
  35. Set byte, short and long data types by using the Prepared Statement
  36. Prepared Statement Set Big Decimal
  37. Set Date by using the Prepared Statement
  38. Set Time by using the Prepared Statement
  39. Set Timestamp by using the Prepared Statement
  40. Copy Table in a MySQL Database