我一直试图连接到我通过Heroku在ClearDB上创建的MySQL数据库,似乎没有运气。
我尝试按照ClearDB文档和Heroku文档中的步骤操作,但我仍然无法连接到它。
目前我面临的错误是:
W/System.err: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at java.lang.reflect.Constructor.newInstance0(Native Method
我在我的libs文件夹中安装了mysql-connector,并将其添加到类路径中(它在我的依赖项中)。这是我的Java代码:
public void writeDatabase(String name, String date, String time) throws Exception {
try {
// Load the driver for MySQL
Class.forName("com.mysql.jdbc.Driver");
/* Setup the connection
jdbc:mysql://my-sql-server/mysql-database?"
+ "user=mysql-username&password=mysql-password
*/
connect = DriverManager.getConnection("jdbc:mysql://my-sql-server/mysql-db?user=mysql-username&password=mysql-password")
statement = connect.createStatement();
resultSet = statement.executeQuery("INSERT INTO user ("+ name + "," + date + "," + time + ") VALUES (?, ?, ?)");
displayData(resultSet);
// Dirty exeption catch
} catch (Exception e) {
throw e;
} finally {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
throw e;
}
}
}
我的SQL服务器似乎是基于我的ClearDB仪表板运行,说它在线。请帮我弄清楚我做错了什么。