无法将 Java ap 连接到在线 MySQL 数据库 - 错误 URL

Cannot connect Java ap to online MySQL database - wrong URL

我开发了一个 Java 应用程序,想将其连接到由 db4free.net 托管的在线 MySQL 数据库。但是我无法通过 JDBC 建立连接,而且我知道我使用的 URL 有问题。我在浏览器中看到的 URL 是:

https://db4free.net/phpMyAdmin/index.php?db=DB_NAME&target=db_structure.php&token=8d68285c6950611a4f9630dc2c132b61

这是我阅读属性的方式:

public class ReadProperties {
    private String db_user;
    private String db_password;
    private String db_connection;
    private Connection connection;

    public ReadProperties(String filename){
        Properties propfile = new Properties();
        InputStream input = null;
        try {
            input = new FileInputStream(filename);
            propfile.load(input);

            db_user = propfile.getProperty("db_user");
            db_password = propfile.getProperty("db_password");
            db_connection = propfile.getProperty("db_connection");

            connection = DriverManager.getConnection( db_connection, db_user, db_password);
            System.out.println(connection);

        } catch (IOException | SQLException ex) {
            ex.printStackTrace();
        } 
        finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

还有我的属性文件:

db_user=USERNAME
db_password=PASSWORD
db_driver=com.mysql.jdbc.Driver
db_connection=jdbc:mysql://db4free.net/DB_NAME

URL 我究竟应该使用什么? 这是我在 phpMyAdmin 中看到的(我已经删除了数据库的名称) :

数据库正在侦听特定端口,您必须指定它,它应该是 3306(正如我在屏幕截图底部看到的那样)。

您的数据库连接 属性 应该是:

db_connection=jdbc:mysql://db4free.net:3306/DB_NAME
  • 首先你应该得到你的IP地址和端口 数据库:
  • 第二次 ping 你的数据库,如果它工作,那么一切都会好起来的:

  • MySQL 的默认端口为 3306,因此您的 URL 或 db_connection 应如下所示:

将您的 URL 更改为:

db_connection=jdbc:mysql://85.10.205.173/DB_NAME
  • 一定要打电话给 Class.forName(driver);

编辑

我已经在这个网站上创建了一个数据库,并且我创建了一个简单的程序并且它与我一起工作:

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

public class CreerConnection {

    static String driver = "com.mysql.jdbc.Driver";
    static String DB_username = "youcef";
    static String DB_password = "youcef";
    static String DB_URL = "jdbc:mysql://85.10.205.173/bd_test";

    public static void main(String[] args) {
        try {
            Class.forName(driver);
            java.sql.Connection con = DriverManager.getConnection(DB_URL, DB_username, DB_password);
            System.out.println("Success");
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Exception : " + e);
        }
    }
}

您可以使用我的数据希望这可以帮助到您。