我开发了一个 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 中看到的(我已经删除了数据库的名称):
最佳答案
- 首先你应该得到你的IP地址和端口 数据库:
- 第二次 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/