JDBC: Create Table Example


 

JDBC: Create Table Example

In this section, we are going to create table using JDBC and using database MySql.

In this section, we are going to create table using JDBC and using database MySql.

JDBC: Create Table Example

In this section, we are going to create table using JDBC and using database MySql.

Create Table : Database table is collection of rows and columns. Columns are also called field and each row shows record. A table contains unique field called primary key which uniquely identify your table record.
For creation of database table we need to understand some terms which are important in creation of table.

Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.

Class.forName(String driver): It loads the driver.

DriverManager  :  The DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property

getConnection(String url+dbName, String userName, String password): This method establishes a connection to specified database url.

The MySQL connection URL has the following format:
jdbc:mysql://[host][:port]/[database][?property1][=value1]...

  • host :The host name where MySQL server is running. Default is 127.0.0.1 - the IP address of localhost.
  • port : The port number where MySQL is listening for connection. Default is 3306.
  • dbName : The name of an existing database on MySQL server. If not specified, the connection starts no current database.
  • Property : The name of a supported connection properties. "userName" and "password" are 2 most important properties.

Statement: This interface executes the SQL statement and returns the result it produces.

createStatement(): It is a method of Connection interface which returns Statement object.

executeUpdate(String table): This method of Statement interface execute sql statements(insert or delete or update) which takes the sql query of string type and returns int.

Example :  

In this example students is our database and we are creating table, named student in the database. For that we create connection to the database using jdbc and write sql query of table creation. If table already exist, then you will get message ("Table is already existed").

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

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 = "students";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driverName);
con = DriverManager.getConnection(url + dbName, userName, password);
try {
Statement st = con.createStatement();
String sql = "CREATE TABLE student(roll_no int,name varchar(30),course varchar(30),PRIMARY KEY(roll_no))";
st.executeUpdate(sql);
System.out.println("Table is created successfully.");
} catch (SQLException s) {
System.out.println("Table is already existed");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output :

Table Creation Example!
Table is created successfully.

Here is your table -

Ads