Wednesday, 5 October 2016

Java Program to create a table in a Database

The following Java Program is an example of creating a table in a database.

package sample;

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

class CreateTable
{
public static void main(String[] args) throws SQLException,ClassNotFoundException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con= DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:xe", "system","kiru");
Statement st=con.createStatement();
if(!st.execute("create table student(sno number(2),sname varchar(10))"))
  System.out.println("table is created");
}
}

Oracle Express DB is used in this example. The folder structure and the output of the following program are shown in the fig below.

0 comments:

Post a Comment