我已经开发了一个Java应用程序,并希望将它连接到一个由MySQL托管的在线db4free.net数据库。但是我无法通过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中看到的(我删除了数据库的名称):

发布于 2017-01-20 08:53:15

将您的URL更改为:
db_connection=jdbc:mysql://85.10.205.173/DB_NAMEClass.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);
}
}
}你可以用我的数据希望这能帮到你。
发布于 2017-01-20 08:49:51
数据库正在监听一个特定的端口,您必须指定它,它应该是3306 (正如我在屏幕截图的底部看到的那样)。
您的db连接属性应该是:
db_connection=jdbc:mysql://db4free.net:3306/DB_NAMEhttps://stackoverflow.com/questions/41759411
复制相似问题