JDBC - creating a database table from Java
Description
My blog post on creating and dropping the database table using JDBC
Shared by: Santhosh_Mandadi
Categories
Tags
-
Stats
- views:
- 16
- posted:
- 6/23/2012
- language:
- English
- pages:
- 2
Document Sample


http://www.javaservletsjspweb.in
Santhosh Reddy Mandadi
Monday, June 11, 2012
Web URL: http://www.javaservletsjspweb.in/2012/06/creating-database-table-from-java.html
Creating a database table from Java
Here the sample program which will demonstrate creating a table in Oracle. Note, table will be dropped
at the end of the program. Just observe the program now and I'll explain the program in details
TableCreationDemo.java
/**
* A demo class to explain database table creation
* @author Santhosh Reddy Mandadi
* @since 08-Jun-2012
* @version 1.0
*/
import java.sql.*;
public class TableCreationDemo
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:@localhost:1521:xe","scott","tiger");
String vsql1, vsql2;
vsql1 = "CREATE TABLE PRODUCTS(PID NUMBER, PNAME VARCHAR(20), PRICE NUMBER(10,2)";
vsql2 = "DROP TABLE PRODUCTS";
Statement statement = connection.createStatement();
statement.executeUpdate(vsql1);
System.out.println("PRODUCTS table has been created. Please check the table in DB...");
System.in.read();System.in.read();
statement.executeUpdate(vsql2);
System.out.println("PRODUCTS table has been dropped...");
}
}
Explanation:
Connection connection =
DriverManager.getConnection("jdbc:oracle:@localhost:1521:xe","scott","tiger");
When this statement is executed, JDBC driver code establishes the connection with the server and
creates a connection object.
http://www.javaservletsjspweb.in
Santhosh Reddy Mandadi
Statement statement = connection.createStatement();
When this statement is executed, JDBC driver code creates the statement object.
statement.executeUpdate(vsql1);
When executeUpdate() method is called the JDBC driver code sends create table statement the
database server. The database server parses the SQL statement and executes the statement. So, table
will be created in DB server.
statement.executeUpdate(vsql2);
When this statement executed, table will be dropped in the DB server.
As explained, this program is operating on the DB server. So, it is called as database client.
Shared by: Santhosh Reddy Mandadi
About
I'm working as a Technical Project Manager at Hotcourses India Private Limited.
Related docs
Other docs by Santhosh_Mandadi