【发布时间】:2018-01-13 08:00:58
【问题描述】:
我正在尝试使用 java httpServlet 和 netbeans 创建我的第一个 API,它们将连接到基于其 examples 和 documentations 的谷歌云上的数据库。我还没有创建数据库,但我获得了必要的凭据和变量来创建连接。所以我从github下载了项目,当我尝试从netbeans打开它时,出现了一些关于依赖项的问题......所以我解决了它们,我用它们的值替换了凭据并运行项目;不幸的是,抛出了一个错误:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. 我进行了一些搜索,但没有得到任何结果......这可能是这段代码的错误吗?如果在云上不可见,数据库安全性会出错吗?任何帮助都将不胜感激。
public class ListTables {
public static void main(String[] args) throws IOException, SQLException {
String instanceConnectionName = "<foo:foo:bar>";
String databaseName = "myDatabaseName ";
String username = "myUsername ";
String password = "<myPass>";
if (instanceConnectionName.equals("<insert_connection_name>")) {
System.err.println("Please update the sample to specify the instance connection name.");
System.exit(1);
}
if (password.equals("<insert_password>")) {
System.err.println("Please update the sample to specify the mysql password.");
System.exit(1);
}
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
databaseName,
instanceConnectionName);
try{
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
/* try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("select * from wf_accounts;");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}*/
}catch(Exception ex){
System.out.println("Error: " + ex.toString());
}
更新
当我这样做时抛出了同样的错误:
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
"",
"");
有什么提示吗?
【问题讨论】:
-
您是否尝试关闭防火墙并尝试连接?
-
也许this 的问题可能对您有所帮助。
标签: java mysql sql-server google-app-engine netbeans