Membuat Koneksi MySQL menggunakan JDBC Driver pada Java

Pada tutorial kali ini, menjelaskan tentang bagaimana membuat koneksi MySQL menggunakan JDBC Driver pada Java. Sebelum melanjutkan ke tahapan berikutnya, pastikan project yang kita buat sudah ditambahkan library MySQL JDBC Driver. Download MySQL JDBC Driver di sini —MySQL JDBC Driver Download Disini.

Baca Juga: membuat Koneksi Ms. Access menggunakan UCanAccess Pada Java

1. Contoh koneksi MySQL menggunakan JDBC Driver pada Java

Berikut code dasar untuk melakukan koneksi ke database MySQL menggunakan JDBC Driver.

1
2
3
4
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
conn.close();

Kode lengkap untuk melakukan koneksi menggunakan JDBC Driver ke Database MySQL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class MysqlJdbcConnection {
 
public static void main(String[] args) {
 System.out.println("-------- MySQL JDBC Connection Testing ------------");
 
try {
 Class.forName("com.mysql.jdbc.Driver");
 } catch (ClassNotFoundException e) {
 System.out.println("Where is your MySQL JDBC Driver?");
 e.printStackTrace();
 return;
 }
 
System.out.println("MySQL JDBC Driver Registered!");
 Connection connection = null;
 
try {
 connection = DriverManager
 .getConnection("jdbc:mysql://localhost:3306/jagungodakwebid", "root", "password");
 
} catch (SQLException e) {
 System.out.println("Connection Failed! Check output console");
 e.printStackTrace();
 return;
 }
 
if (connection != null) {
 System.out.println("You made it, take control your database now!");
 } else {
 System.out.println("Failed to make connection!");
 }
 }
 
}

2. Output

Asumsi pada percobaan kali ini, menggunakan Netbeans dengan nama project Jagungodakwebid dengan library MySQL JDBC Driver.

-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
You made it, take control your database now!

Note :

Jika terjadi error seperti screenshoot dibawah, hal ini disebabkan karena library MySQL JDBC Driver belum ditambahkan ke dalam Library Project. Download MySQL JDBC Driver disini

Where is your MySQL JDBC Driver?
 at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:188)
 at MysqlJdbcConnection.main(MysqlJdbcConnection.java:12)

Berikutnya, jika terjadi error seperti screenshoot berikut, hal ini disebabkan karena database tidak ditemukan atau Service database MySQL belum di aktifkan. Pastika service database MySQL aktif dan database dengan nama jagungodakwebid telah dibuat.

-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
Connection Failed! Check output console
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
 at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
 at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
 at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
 at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
 at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
 at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
 at java.sql.DriverManager.getConnection(DriverManager.java:579)
 at java.sql.DriverManager.getConnection(DriverManager.java:221)
 at MysqlJdbcConnection.main(MysqlJdbcConnection.java:24)
Caused by: java.net.ConnectException: Connection refused: connect
 at java.net.DualStackPlainSocketImpl.connect0(Native Method)
 at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
 at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
 at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
 at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
 at java.net.Socket.connect(Socket.java:579)
 at java.net.Socket.connect(Socket.java:528)
 at java.net.Socket.<init>(Socket.java:425)
 at java.net.Socket.<init>(Socket.java:241)
 at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
 at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
 ... 15 more

Refarence

  1. Connect to MySQL with JDBC Driver
  2. JDBC Tutorial

 

Share This

2 tanggapan untuk “Membuat Koneksi MySQL menggunakan JDBC Driver pada Java

  1. Ping-balik: Membuat Koneksi Ms. Access menggunakan UCanAccess pada Java

  2. Ping-balik: Membuat Koneksi Ms. Access menggunakan UCanAccess pada Java - Jagungodak

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

3 + 4 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.