简体   繁体   中英

MySQL database unable to connection

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;
        }
    }
}

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, eg c3p0 , dbcp , some properties like validateQuery and testOnBorrow are quite effective for this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM