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

标签 java mysql url jdbc database-connection

我开发了一个 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 中看到的(我已经删除了数据库的名称):

enter image description here

最佳答案

  • 首先你应该得到你的IP地址和端口 数据库:
  • 第二次 ping 你的数据库,如果它工作,那么一切都会好起来的,就像这样:

ping

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

将您的网址更改为:

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

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

关于java - 无法将 Java ap 连接到在线 MySQL 数据库 - 错误的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41759411/

相关文章:

java - 从数组中获取随机值

mysql - 仅当另一行中存在值时才选择一行

c# - Windows 8 手机 C# 请求桌面网站无法正常工作

带有 URL 重写的页面上的 PHP 函数?

具有哈希的 Facebook 共享 URL

java - 创建返回类型的静态实例变量

java - 如何在 Hibernate 中通过第三个实体将一个实体连接到第二个实体?

mysql - Laravel Eloquent 组按最新记录

php - 当名字和姓氏在不同字段中时,使用 PHP 在数据库中搜索全名

java - 无法使用注释在 Spring IOC 中创建请求范围 bean