我想创建我的第一个API使用Java httpServlet和netbeans将被连接到谷歌云上的数据库的基础上它的examples和documentations。我没有创建数据库,但我获得了必要的证书和变量来创建连接。所以我从github上下载了这个项目,当我试图从netbeans打开它时,出现了一些与依赖有关的问题......所以我解决了它们,我用它们的值替换了证书并运行项目;不幸的是发生了一个错误:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
我做了一些搜索,但没有得到任何结果...可能是这个代码的错误?数据库安全性错误,如果它在云上不可见?任何帮助都不胜感激。“无法创建连接到数据库服务器”netbeans谷歌云错误
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
"",
"");
任何提示:
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());
}
更新
我这样做时,被抛出同样的错误?
你有没有试图关闭您的防火墙,试图连接? –
也许[this](https://stackoverflow.com/questions/34633515/connection-made-to-google-cloud-sql-drops-intermittently)问题可能会对您有所帮助。 –