Java - 无法连接到远程数据库

时间:2015-09-28 14:44:15

标签: java jdbc

我尝试使用JDBC连接器将我的应用程序连接到远程服务器数据库,但每次都会出现连接错误。

ConnexionBDD.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnexionBDD {

private static final String dbURL = "jdbc:mysql://**my_server_ip_address**/**my_db_name**:3306?useUnicode=yes&characterEncoding=UTF-8&connectTimeout=3000";
private static final String user = "**my_username**";
private static final String password = "**my_password**";
private static Connection connexion = null;

/**
 * Connexion a la base de donnees; chargement du driver
 * 
 * @throws ClassNotFoundException
 * @throws SQLException
 */
private ConnexionBDD() throws ClassNotFoundException, SQLException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        try {
            connexion = DriverManager.getConnection(dbURL, user, password);
            System.out.println("Connexion BDD Ok");
        } catch (SQLException e) {
            System.out.println("Erreur Connexion BDD : " + e.getMessage());
        }
    } catch (ClassNotFoundException e) {
        System.out.println("Erreur Chargement Driver" + e.getMessage());
    }
}

/**
 * Recuperation de la connexion
 * 
 * @return la connexion
 * @throws ClassNotFoundException
 * @throws SQLException
 */
public static Connection getConnexion() throws ClassNotFoundException, SQLException {
    if (connexion == null) {
        new ConnexionBDD();
    }
    return connexion;
}
}

当我尝试调用getConnexion()方法时:

Connection connexion = ConnexionBDD.getConnexion() ;

我将此错误视为输出:

Erreur Connexion BDD : 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.

请注意,当我尝试在本地服务器上连接时,它可以正常工作,但不能在远程服务器上运行..

0 个答案:

没有答案