使用Java访问联机数据库 [英] Accessing online database with Java

查看:196
本文介绍了使用Java访问联机数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个访问数据库的Java程序。在开发阶段,我使用了本地数据库(XAMPP with MySQL),但是当我试图访问2个不同的在线数据库(一个商业和免费的)时,我收到以下两种情况下的例外:

I made a Java program which accesses a database. During the development phase I used a local database (XAMPP with MySQL), but when I tried to access 2 different online databases (a commercial and a free one), I received in both cases the following exception:


SQLException:通信链路失败

成功发送到服务器的最后一个数据包为0毫秒
前。

SQLException: 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.

我没有用PHP接受任何问题。

I hadn't any problems acessing them with PHP.

这是我的代码:

private String user = "user1";
private String pass = "pass1";
private String dbClass = "com.mysql.jdbc.Driver";
private String dbDriver = "jdbc:mysql://db4free.net:3306/DBNAME";
private Connection conn = null;

public boolean connect() {
    boolean done = false;
    //load driver
    try {
        Class.forName(dbClass).newInstance();
        System.out.println("driver loaded"); // THIS IS BEING RETURNED
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        System.err.println(ex);
    }
    // Connection
    try {
        conn = DriverManager.getConnection(dbDriver, user, pass);
        System.out.println("connected"); // THIS IS NOT BEING RETURNED
        done = true;
    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
    }
    return done;
}


推荐答案

的防火墙问题,通常服务器可能正在侦听127.0.0.1的ip地址,所以你只从本地系统访问我的sql,将其更改为0.0.0.0在配置文件,然后它将开始工作。

I think it might be because of firewall issue, usually the server might be listening on the 127.0.0.1 ip address, so you access my sql only from the local system, change it to 0.0.0.0 in the configuration file, then it will start working.

这篇关于使用Java访问联机数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆