Monday, May 24, 2010

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:

It is a interface. Statement object executes the SQL statement and returns the result it produces.

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";
con = DriverManager.getConnection(url+dbName, userName, password);
Statement st = con.createStatement();
String table = "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))";
System.out.println("Table creation process successfully!");
catch(SQLException s){
System.out.println("Table all ready exists!");
catch (Exception e){

No comments:

Post a Comment