Java MySql通信链接失败

时间:2016-05-27 06:15:39

标签: java mysql jdbc netbeans netbeans-8

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.

我投降我已经尽了最大努力请帮助我,我只需要连接到我的数据库

我使用hostinger.ph作为我的数据库的托管网站

DBconnect.java

package mftis;

import java.sql.*;

public class DBconnect {
private Connection con;
private Statement st;
private ResultSet rs;

public DBconnect(){
try{
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Success in using the Driver");

    con =            DriverManager.getConnection("jdbc:mysql://mysql.hostinger.ph/u366906409_mftis","u366906409_mftis","mftis2016");
    st = con.createStatement();

}catch(Exception e){
    System.out.println(e);
}
}

}

login_window

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    DBconnect connect = new DBconnect();
}

请相信我的帮助 无论如何,我的数据库是通过互联网我可以找到的所有教程都使用localhost

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

Class.forName(driverClass).newInstance();
        Properties connProp = new Properties();
        connProp.setProperty("user", properties.getDbUser());
        connProp.setProperty("password", properties.getDbPasswd());
        connProp.setProperty("connectTimeout", timeout);
        connProp.setProperty("socketTimeout", timeout);
        return DriverManager.getConnection(dburl,  connProp);

dburl是:

String url = "" + properties.getDbServer() + ":" + properties.getDbPort() + "/" + properties.getDbName();

            url = "jdbc:mysql://" + url + "?zeroDateTimeBehavior=convertToNull";

        return url;