Database sangat diperlukan dalam membuat suatu aplikasi , apalagi pada kelas enterprise. Kali ini saya akan share bagaimana caranya membuat koneksi antara database MySql dan Java pada Netbeans.
Mengapa saya lebih condong ke MySql ?
Jawabannya simple, selain lebih familiar, MySql lebih luas pemakainnya, karena bisa menggunakan akses internet, dan bisa diletakkan di cloud storage atau hosting.
Pertama kamu harus membuat sebuah class baru, beri nama class "JMysql".
Setelah itu, kita membuat codingan seperti dibawah ini.
Jangan lupa saat mengetikkan tekan ctrl+space agar netbeans otomatis mengimport apa saja yang diperlukan.
Berikut adalah source code nya, bisa diedit sesuai kebutuhan, ubah nama database sesuai database yang telah dibuat. dan alamat database yang sesuai, di sini saya menggunakan alamat //localhost.
Mengapa saya lebih condong ke MySql ?
Jawabannya simple, selain lebih familiar, MySql lebih luas pemakainnya, karena bisa menggunakan akses internet, dan bisa diletakkan di cloud storage atau hosting.
Pertama kamu harus membuat sebuah class baru, beri nama class "JMysql".
![]() |
Create Java Class... |
Jangan lupa saat mengetikkan tekan ctrl+space agar netbeans otomatis mengimport apa saja yang diperlukan.
![]() |
Contoh Otomatis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JMysql { | |
public static Connection getkoneksi(){ | |
Connection cn=null; | |
try { | |
cn=DriverManager.getConnection("jdbc:mysql://localhost/namaDatabase","root",""); | |
} catch (SQLException e) { | |
JOptionPane.showMessageDialog(null,"Gagal menyambungkan koneksi : "); | |
} | |
return cn; | |
} | |
public static void closekoneksi (Connection cn, PreparedStatement pr, ResultSet rs){ | |
try { | |
if (cn !=null){ | |
cn.close(); | |
} | |
if(pr !=null){ | |
pr.close(); | |
} | |
if(rs !=null){ | |
rs.close(); | |
} | |
} catch (SQLException e) { | |
JOptionPane.showMessageDialog(null,"Gagal memutuskan koneksi : "+e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connection cn = JMysql.getkoneksi(); | |
PreparedStatement pr; | |
ResultSet rs ; | |
String sql="select *from barang"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JMysql.closekoneksi(cn, pr, rs); |
Koneksi database MySql dan Java pada Netbeans
4/
5
Oleh
Unknown