Thursday, June 5, 2025

.::: Simulation connection java to MariaDB for Success & Fail using mysql-connector-j-8.0.33.jar (MySQL Driver) (com.mysql.cj.jdbc.exceptions.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.) :::..

 

1. Download (Platform independent) MySQL Driver

https://repo1.maven.org/maven2/com/mysql/mysql-connector-j/8.0.33/mysql-connector-j-8.0.33.jar

java -version
dnf install java-17-openjdk-devel -y    # CentOS 9


2. check login mariadb

[root@teguhth ~]# mysql -h 10.10.10.90 -uadmin -p teguhth
mysql: Deprecated program name. It will be removed in a future release, use '/usr/bin/mariadb' instead
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 11.4.4-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [teguhth]> exit
Bye
[root@teguhth ~]#
[root@teguhth ~]# mysql  -uadmin -p teguhth
mysql: Deprecated program name. It will be removed in a future release, use '/usr/bin/mariadb' instead
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 11.4.4-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [teguhth]>

 


3. create script java 

[root@teguhth mysql]# cat MariaDBTest.java

import java.sql.*;

public class MariaDBTest {
    public static void main(String[] args) {
        String url = "jdbc:mysql://10.10.10.90:3306/teguhth"; // IP tidak bisa dijangkau
        String user = "admin";
        String pass = "admin";

        try {
            Connection conn = DriverManager.getConnection(url, user, pass);
            System.out.println("Koneksi berhasil.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
[root@teguhth mysql]#


4. run script 

javac -cp .:mysql-connector-j-8.0.33.jar MariaDBTest.java

5. after create class run 

javac -cp .:mysql-connector-j-8.0.33.jar MariaDBTest.java
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest

[root@teguhth mysql]# javac -cp .:mysql-connector-j-8.0.33.jar MariaDBTest.java
[root@teguhth mysql]# java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
Koneksi berhasil.
[root@teguhth mysql]#


6. if fail 

[root@teguhth mysql]# java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:175)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:446)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:239)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:188)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
        at MariaDBTest.main(MariaDBTest.java:11)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:62)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:150)
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:166)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
        at com.mysql.cj.NativeSession.connect(NativeSession.java:121)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:945)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:815)
        ... 6 more
Caused by: java.net.NoRouteToHostException: No route to host
        at java.base/sun.nio.ch.Net.connect0(Native Method)
        at java.base/sun.nio.ch.Net.connect(Net.java:579)
        at java.base/sun.nio.ch.Net.connect(Net.java:568)
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:593)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
        at java.base/java.net.Socket.connect(Socket.java:633)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
        ... 9 more
[root@teguhth mysql]#

 

7. if success

[root@teguhth mysql]# javac -cp .:mysql-connector-j-8.0.33.jar MariaDBTest.java
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
Koneksi berhasil.
[root@teguhth mysql]#

 

8. if driver not found
[root@teguhth mysql]# javac -cp .:mysql-connector-j-8.0.34.jar MariaDBTest.java
[root@teguhth mysql]# java -cp .:mysql-connector-j-8.0.34.jar MariaDBTest
java.sql.SQLException: No suitable driver found for jdbc:mysql://10.10.10.90:3306/teguhth
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
        at MariaDBTest.main(MariaDBTest.java:11)
[root@teguhth mysql]#

 

9. Test speed 
sh durajavamaria.sh

[root@teguhth mysql]# sh durajavamysql.sh
 


[root@teguhth mysql]# cat durajavamysql.sh
#!/bin/bash

# Menyimpan waktu mulai dalam format manusiawi
# For Test
# timedatectl set-time "2020-03-22 08:34:00"

datex=$(date "+%Y-%m-%d %H:%M:%S.%N")
#epoch_start='1580390912'
epoch_start=$(date +%s)  # Gunakan detik saja, jangan nanodetik
echo "$datex ->>> Start Process"

# For Test
# timedatectl set-time "2025-01-31 08:34:00"
# vmware-toolbox-cmd timesync enable

######## Script Begin ########

echo "run 10x 'java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest' using MySQL Driver Connector/J"

java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest
java -cp .:mysql-connector-j-8.0.33.jar MariaDBTest

######## Script Finish ########

# Menyimpan waktu selesai dalam format manusiawi
datey=$(date "+%Y-%m-%d %H:%M:%S.%N")
epoch_end=$(date +%s)  # Gunakan detik saja
#epoch_end='1738253312'

echo "$datey ->>> Finish Process"

# Menghitung selisih waktu dalam detik
finish=$((epoch_end - epoch_start))

# Menghitung hari, jam, menit, dan detik
days=$((finish / 86400))
hours=$(((finish % 86400) / 3600))
minutes=$(((finish % 3600) / 60))
seconds=$((finish % 60))

# Konversi ke tahun, bulan, hari, jam, menit, dan detik
yearsx=$((finish / 31536000))             # 1 tahun = 365 * 24 * 3600 detik
remaining=$((finish % 31536000))

monthsx=$((remaining / 2592000))          # 1 bulan = 30 * 24 * 3600 detik
remaining=$((remaining % 2592000))

daysx=$((remaining / 86400))              # 1 hari = 86400 detik
remaining=$((remaining % 86400))

hoursx=$((remaining / 3600))              # 1 jam = 3600 detik
remaining=$((remaining % 3600))

minutesx=$((remaining / 60))              # 1 menit = 60 detik
secondsx=$((remaining % 60))

# Format output
formatted_time=$(printf "%d:%02d:%02d:%02d" "$days" "$hours" "$minutes" "$seconds")

# Format output
formatted_time2=$(printf "%d Tahun, %d Bulan, %d Hari, %02d:%02d:%02d" "$yearsx" "$monthsx" "$daysx" "$hoursx" "$minutesx" "$secondsx")

echo "Duration Process"
echo "Start     : $datex"
echo "Finish    : $datey"
echo "Duration 1 : $formatted_time"
echo "Duration 2 : $formatted_time2"
echo ""
echo "Copyright by           : Teguh Triharto"
echo "Website                : https://www.linkedin.com/in/teguhth"
echo ""
[root@teguhth mysql]#


No comments:

Post a Comment

Popular Posts