Potongan Program
Di bawah ini adalah potongan program (program snippet) Java menggunakan JDBC untuk proses delete record di database:Statement statement = connection.createStatement(); // execute the delete SQL stetement statement.executeUpdate(deleteSQL);
Contoh Program
Dibawah ini adalah contoh program penggunaan JDBC untuk delete sebuah record di database. Langkah-langkahnya adalah sebagai berikut:- Login ke Database MySQL
Anda dapat menggunakan perintah mysql -u root -p untuk login ke database MySQL.
# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.36 MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- Gunakan database akademik.
mysql> use akademik; Database changed
- Tampilkan data dari tabel TBL_USER
mysql> SELECT * FROM tbl_user; +---------+---------------------+---------------+------------+--------------+ | USER_ID | USER_NAME | USER_PASSWORD | CREATED_BY | CREATED_DATE | +---------+---------------------+---------------+------------+--------------+ | 001 | Nursalim Al Barbasy | nursalimxx | admin | 2014-10-10 | +---------+---------------------+---------------+------------+--------------+ 1 row in set (0.00 sec)
- Buat program Java untuk menghapus atau Delete data dari TBL_USER yang mempunyai user_id '001'
Nama File: MySQLDeleteRecordTableDemo.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class MySQLDeleteRecordTableDemo { private static final String DB_DRIVER = "com.mysql.jdbc.Driver"; private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/akademik"; private static final String DB_USER = "root"; private static final String DB_PASSWORD = ""; public static void main(String[] args){ try { deleteRecord(); } catch (SQLException ex) { ex.printStackTrace(); } } public static void deleteRecord() throws SQLException{ Connection connection = null; Statement statement = null; int countRecordDeleted = 0; String deleteTableQuery = "DELETE FROM TBL_USER WHERE USER_ID = '001'"; try { connection = getDatabaseConnection(); statement = connection.createStatement(); System.out.println(deleteTableQuery); // execute delete SQL stetement statement.executeUpdate(deleteTableQuery); countRecordDeleted = statement.getUpdateCount(); System.out.println(countRecordDeleted+" Record berhasil di delete dari TBL_USER!"); } catch (SQLException ex) { System.out.println(ex.getMessage()); } finally { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } } public static Connection getDatabaseConnection() { Connection connection = null; try { Class.forName(DB_DRIVER); } catch (ClassNotFoundException ex) { System.out.println(ex.getMessage()); } try { connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); return connection; } catch (SQLException ex) { System.out.println(ex.getMessage()); } return connection; } }
Coba Anda jalankan program Java diatas, apabila tidak ada error maka akan menampilkan output seperti dibawah ini:
DELETE FROM TBL_USER WHERE USER_ID = '001' 1 Record berhasil di delete dari TBL_USER!
- Tampilkan kembali data dari tabel TBL_USER.
mysql> SELECT * FROM tbl_user; Empty set (0.00 sec)
Pada hasil output diatas, terlihat bahwa data di table TBL_USER dengan USER_ID = '001' berhasil dihapus.
Sekian tutorial singkat tentang Bagaimana Cara Delete Record Di Database MySQL Menggunakan JDBC Statement. Semoga bermanfaat & Selamat Belajar JDBC (Java Data Base Connectivy).
Jika Anda menyukai tutorial ini, silahkan anda share dan bagikan dengan teman-teman Anda, dan jika Anda ingin berdiskusi tentang judul tutorial diatas, atau pun mau bertanya tentang tutorial ini silahkan Anda untuk menggunakan fasilitas komentar untuk berdiskusi dan bertanya di blog ini.
Salam,
~ Nursalim ~
Naura-Lab.blogspot.com
Post a Comment