0

I have a private vps, the vps is ubuntu 12.04 using zpanel for phpmyadmin, set up to running a MySQL database already but when I try to connect to the database with Java I am unable to and receive the message:

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."

This is my connection code:

package net;

import java.sql.*;

import javax.swing.JOptionPane;

public class LoginDatabaseConnection {
    Connection conn = null;

    public static Connection ConnectDB() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://vps35560.vps.ovh.ca/zadmin_login", "*masked*","*masked*");
            System.out.print("Connection Establish");
            return conn;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
            System.out.println(e);
            return null;
        }
    }
}
Mike Nguyen
  • 73
  • 1
  • 1
  • 8

1 Answers1

0

Maybe the reason for this problem is DB connection was destroyed, here are some ways for your issue,
1. append autoReconnect=true to the database connection url.
2. due to it's 8 hours long of wait_timeout by default for MySQL, so try to modify the configuration file my.ini to make it even longer, then restart MySQL service.
3. it's strongly recommended to use a database source pool, e.g. c3p0, dbcp, some properties like validateQuery and testOnBorrow are quite effective for this problem.

Hunter Zhao
  • 4,589
  • 2
  • 25
  • 39
  • I've added the autoReconnect=true but it still doesnt work The wait_timeout i believe I've changed it to 24 hours I do not know what those are so can you please elaborate on them for me? Also I am using MySQL connector from http://dev.mysql.com/downloads/connector/j/ if it helps – Mike Nguyen Jan 01 '15 at 01:13
  • just try the other two ways, and the the 3rd way works for your application absolutely. @Mike Nguyen – Hunter Zhao Jan 01 '15 at 01:17
  • I've done way 1 & 2 but am confuse about how to do way 3 – Mike Nguyen Jan 01 '15 at 03:21
  • maybe adding that the vps is ubuntu 12.04 using zpanel for phpmyadmin might help – Mike Nguyen Jan 01 '15 at 03:28