Persiapan
- Berdoa sebelum melakukan aktifitas
- Download dan Install JDK (Java Development Kit)
- Download Oracle JDBC Driver
- Java Editor (Eclipse)
- Kopi dan snack untuk menemani Anda beraktifitas,
Program Snippet
Di bawah ini adalah potongan program untuk membuat table di database Oracle via JDBC (Java Data Base Connectivy) menggunakan Prepared Statement.
PreparedStatement ps = connection.prepareStatement(createTableQuery); // EXECUTE Create SQL Statement ps.executeUpdate();
Contoh Program
Di bawah ini adalah contoh lengkan program Java untuk membuat table di Database Oracle menggunakan PreparedStatement.
package com.nursalim.jdbc.oracle; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class JDBCOracleCreateTable { private static final String DB_CONNECTION_URL = "jdbc:oracle:thin:@localhost:1521:ORCL"; private static final String DB_USER = "nano"; private static final String DB_PASSWORD = "nano"; private static String createTableQuery = "CREATE TABLE PEGAWAI(" + "ID_PEGAWAI VARCHAR2(10 BYTE) NOT NULL, " + "NAMA_PEGAWAI VARCHAR2(30 BYTE) NOT NULL, " + "UMUR NUMBER(3) NOT NULL, " + "ALAMAT VARCHAR2(30 BYTE) NOT NULL, " + "GENDER CHAR(1 BYTE) NOT NULL, " + "DEPARTEMEN VARCHAR2(5 BYTE) NOT NULL, " + "PRIMARY KEY (ID_PEGAWAI) " + ")"; public static void main(String[] args) { createTable(createTableQuery); } public static void createTable(String query){ Connection connection = null; PreparedStatement ps = null; connection = getConnection(); try { ps = connection.prepareStatement(query); System.out.println(query); ps.executeUpdate(); System.out.println("Table \"Pegawai\" successfully created!"); } catch (SQLException ex) { ex.printStackTrace(); } } public static Connection getConnection() { Connection connection = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("Oracle JDBC Driver has been succesfully registered"); } catch (ClassNotFoundException e) { System.out.println("Oracle JDBC Driver not found"); System.out.println(e.getMessage()); } try { connection = DriverManager.getConnection(DB_CONNECTION_URL, DB_USER,DB_PASSWORD); System.out.println("Successfully connected to oracle database"); return connection; } catch (SQLException ex) { System.out.println("Cannot connect oracle database"); ex.printStackTrace(); } return connection; } }
Running
Coba Anda compile dan jalankan program Java diatas, kalau tidak ada error maka akan keluar output seperti di bawah ini:
Oracle JDBC Driver has been succesfully registered Successfully connected to oracle database CREATE TABLE PEGAWAI(ID_PEGAWAI VARCHAR2(10 BYTE) NOT NULL, NAMA_PEGAWAI VARCHAR2(30 BYTE) NOT NULL, UMUR NUMBER(3) NOT NULL, ALAMAT VARCHAR2(30 BYTE) NOT NULL, GENDER CHAR(1 BYTE) NOT NULL, DEPARTEMEN VARCHAR2(5 BYTE) NOT NULL, PRIMARY KEY (ID_PEGAWAI) ) Table "Pegawai" successfully created!
Untuk membuktikan apakah table "Pegawai" telah berhasil Anda buat, Anda dapat menggunakan perintah di bawah ini melalui perintah SQL.
SQL> desc pegawai; Name Null? Type ----------------------------------------- -------- ----------------------- ID_PEGAWAI NOT NULL VARCHAR2(10) NAMA_PEGAWAI NOT NULL VARCHAR2(30) UMUR NOT NULL NUMBER(3) ALAMAT NOT NULL VARCHAR2(30) GENDER CHAR(1) DEPARTEMEN VARCHAR2(5)
~~~## JDBC Tutorial ##~~~
Sekian tutorial singkat tentang Cara Membuat Table Di Database Oracle via JDBC PreparedStatement. Semoga bermanfaat & Happy Learning JDBC.
0 comments:
Post a Comment