Potongan Program
Di bawah ini adalah potongan program Java (program snippet) untuk meng-update record di suatu table menggunakan JDBC.Statement statement = connection.createStatement(); // execute the update SQL stetement statement.executeUpdate(updateSQL);
Contoh Program
Sebelum Anda membuat program Java menggunakan JDBC untuk meng-update record di table, sebaiknya Anda harus mengecek atau menampilkan data yang akan anda update terlebih dahulu. Langkah-langkahnya adalah sebagai berikut:- Login ke database MySQL Anda.
# 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.
- Pilih 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 Alfarizi | nursalimxx | admin | 2014-10-10 | +---------+-------------------+---------------+------------+--------------+ 1 row in set (0.05 sec)
- Buat Program JDBC untuk meng-update TBL_USER yang mempunyai user_id = '001'.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class MySQLUpdateTableDemo { 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 { updateRecord(); } catch (SQLException ex) { ex.printStackTrace(); } } public static void updateRecord() throws SQLException { Connection connection = null; Statement statement = null; int recordCount = 0; String updateTableQuery = "UPDATE TBL_USER" + " SET USER_NAME = 'Nursalim Al Barbasy' " + " WHERE USER_ID = '001'"; try { // membuat object connection connection = getDatabaseConnection(); // membuat object statement statement = connection.createStatement(); System.out.println(updateTableQuery); // eksekusi update table query statement.execute(updateTableQuery); // tampilkan jumlah record yang ter-update recordCount = statement.getUpdateCount(); System.out.println(recordCount + " Record berhasil di update di tabel TBL_USER"); } catch (SQLException e) { System.out.println(e.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 diatas, apabila tidak ada error maka akan menghasilkan output seperti dibawah ini:
UPDATE TBL_USER SET USER_NAME = 'Nursalim Al Barbasy' WHERE USER_ID = '001' 1 Record berhasil di update di tabel TBL_USER
- Tampilkan kembali 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)
Referensi
What Next?
Sekian tutorial singkat Bagaimana Cara Update Table Di 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