Pada tutorial kali ini saya akan membahas tentang JDBC, (Java database connectivity) adalah spesifikasi standar dari JavaSoft API (Aplication Programming Interface) yang memungkinkan program Java untuk mengakses sistem database manajemen. JDBC menyediakan methods untuk melakukan query dan modifikasi data pada RDBMS seperti Oracle, SQL Server, MySQL, dll menggunakan Driver Manager. JDBC mirip dengan ODBC (Open Database Connectivity), hanya saja JDBC spesifik digunakan untuk program Java. ODBC bersifat language independent.
JDBC memiliki beberapa kegunaan, yaitu membantu programmer untuk koneksi dengan data source, membantu mengirimkan query dan update statements ke database, dan menerima serta memroses hasil yang didapatkan dari database sesuai dengan query yang diberikan.
Ikuti langkah-langkah berikut ini:
Buat database mysql dengan nama db_mahasiswa
Buat table dengan nama mahasiswa dengan atribut nim, nama, prodi, alamat, no_hp
Isi data pada table mahasiswa sesuai identitas anda
Buat Connection.java
Tambahkan library MySQL JDBC Driver
Masukan kode berikut:
Buat Connection.java
Tambahkan library MySQL JDBC Driver
Masukan kode berikut:
import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; public class ConnectionJDBC { Statement st; Connection conn; ResultSet rs; public void connect(){ try { // register jdbc driver Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException ex) { Logger.getLogger(ConnectionJDBC.class.getName()).log(Level.SEVERE, null, ex); } try { conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/db_mahasiswa","root",""); System.out.println("Koneksi ke database db_mahasiswa berhasil"); } catch (SQLException ex) { Logger.getLogger(ConnectionJDBC.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Koneksi gagal"); } } public void select(){ try { //excute query st = (Statement) conn.createStatement(); } catch (SQLException ex) { Logger.getLogger(ConnectionJDBC.class.getName()).log(Level.SEVERE, null, ex); } try { rs = st.executeQuery("SELECT * FROM mahasiswa"); } catch (SQLException ex) { Logger.getLogger(ConnectionJDBC.class.getName()).log(Level.SEVERE, null, ex); } } public void printed() throws SQLException{ while (rs.next()) { System.out.println("-----------------------------------------"); System.out.println("nim : " +rs.getString("nim")); System.out.println("nama : " +rs.getString("nama")); System.out.println("prodi : " +rs.getString("prodi")); System.out.println("alamat : " +rs.getString("alamat")); System.out.println("no_hp : " +rs.getString("no_hp")); System.out.println("-----------------------------------------"); } } public static void main(String[] args) { ConnectionJDBC mc = new ConnectionJDBC(); mc.connect(); mc.select(); try { mc.printed(); } catch (SQLException ex) { Logger.getLogger(ConnectionJDBC.class.getName()).log(Level.SEVERE, null, ex); } } }
Klik kanan pada project dan Run File, berikut hasilnya:
Selanjutnya buat Class baru dengan nama InsertMahasiswa.java
Gunakan perintah insert database melalui java
Berikut codenya:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class InsertMahasiswa {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/db_mahasiswa";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "";
public static void main(String[] args) {
try {
insertTable();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void insertTable() throws SQLException {
Connection connection = null;
Statement statement = null;
int count = 0;
String insertTableSQL = "INSERT INTO mahasiswa"
+ "(nim, nama, prodi, alamat, no_hp) " + "VALUES"
+ "('1500631012','Denny Irvantoro','Teknik Informatika','Jember', "
+ "'083205569512'" +")";
try {
// dapatkan database connection
connection = getDatabaseConnection();
// membuat object connection
statement = connection.createStatement();
System.out.println(insertTableSQL);
// eksekusi insert SQL stetement
statement.executeUpdate(insertTableSQL);
// mendapat total record yang telah di-insert
count = statement.getUpdateCount();
System.out.println(count +" data sudah dimasukan ke tabel mahasiswa");
} 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 e) {
System.out.println(e.getMessage());
}
try {
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
DB_PASSWORD);
return connection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return connection;
}
}
Lalu klik Run FileBerikut hasilnya:
Buat Class baru dengan nama UpdateMahasiswa.java
Masukan code berikut:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateMahasiswa {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/db_mahasiswa";
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 mahasiswa"
+ " SET nama = 'Mochamad Denny Wahyudi' "
+ " WHERE nim = '1500631011'";
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
+ " data sudah diupdate di tabel mahasiswa");
} 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;
}
}
Klik Run File pada project, berikut hasilnya:
Yang terakhir gunakan perintah delete database melalui java
Buat Class lagi dengan nama DeleteMahasiswa.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteMahasiswa {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/db_mahasiswa";
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 mahasiswa WHERE nim = '1500631012'";
try {
connection = getDatabaseConnection();
statement = connection.createStatement();
System.out.println(deleteTableQuery);
// execute delete SQL stetement
statement.executeUpdate(deleteTableQuery);
countRecordDeleted = statement.getUpdateCount();
System.out.println("---------------------------------------");
System.out.println(countRecordDeleted+" data sudah dihapus di tabel mahasiswa");
} 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;
}
}
Klik Run File pada project, dan berikut hasilnya:
Keyword:
- contoh jdbc insert update dan delete pada java dengan mysql
- program jdbc insert update dan delete pada java dengan mysql
- cara menampilkan jdbc insert update dan delete pada java dengan mysql
- download crud java console
- matakuliah pbo
- jdbc
- java
0 Response to "Contoh JDBC Insert Update, dan Delete pada Java dengan MySQL"
Posting Komentar