In this tutorial we will learn how to Execute CREATE table query(DDL command) using java.sql.Statement's executeUpdate method in java JDBC.
What is Statement in JDBC (java database connectivity)?
- The java.sql.Statement object used for executing a static SQL statement and returning the results it produces.
- Statement cannot accept parameters at runtime in java JDBC.
- Statement is slower as compared to PreparedStatement in java JDBC.
- Statement can’t be used for storing/retrieving image and file in database (i.e. using BLOB, CLOB datatypes) in java JDBC.
- Statement enforces SQL injection, because we end up using query formed using concatenated SQL strings in java JDBC.
Statement Example in java JDBC >
stmt = con.createStatement();
stmt.executeUpdate("DELETE from EMPLOYEE where ID=2 ");
- same SQL query can’t be executed repeatedly in Statement .
- Statement makes code less readable and understandable - We may need to write concatenated SQL strings
- java.sql.Statement is an interface in java JDBC.
- By default, only one ResultSet object per Statement object is allowed to be opened at the same time in java JDBC.
- java.sql.Statement Important methods in java JDBC -
java.sql.Statement's executeUpdate method can be used for executing CREATE table queries in java JDBC.
--Before executing java program execute these database scripts >
None
--If table already exists then execute the DROP command >
drop table EMPLOYEE;
If table already exist and below program is executed then you will face error in creating table (ORA-00955: name is already used by an existing object)
Must read : java.sql.Statement - using executeUpdate and executeQuery methods SELECT,INSERT, UPDATE, DELETE
Example/ Full Programs JDBC- Execute CREATE table query using Statement's executeUpdate method in java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class StatementCreateExample {
public static void main(String... arg) {
Connection con = null;
Statement stmt = null;
try {
// registering Oracle driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
// getting connection
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"ankit", "Oracle123");
System.out.println("Connection established successfully!");
stmt = con.createStatement();
//execute create table query
stmt.executeUpdate("create table EMPLOYEE("
+ "ID number(4), NAME varchar2(22))");
System.out.println("EMPLOYEE Table created");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
if(stmt!=null) stmt.close(); //close Statement
if(con!=null) con.close(); // close connection
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/*OUTPUT
Connection established successfully!
EMPLOYEE Table created
*/
|
So, in this tutorial we learned how to Execute CREATE table query(DDL command) using Statement's executeUpdate method in java JDBC.
RELATED LINKS>
Oracle 11g and SQL Developer installation and setup on 32/64bit windows - explained step by step with screenshots
java.sql.PreparedStatement - using executeUpdate and executeQuery methods - CREATE, SELECT, INSERT, UPDATE and DELETE
JDBC- Insert/Store/save FILE in database
JDBC- Batch PreparedStatement example- Execute INSERT query(DML command) using PreparedStatement's addBatch() and executeBatch() methods in java
Execute database STORED PROCEDURE - IN parameter, OUT parameter and IN OUT parameter || call FUNCTION from java