2017-08-05 101 views
0

我想创建我的第一个API使用Java httpServlet和netbeans将被连接到谷歌云上的数据库的基础上它的examplesdocumentations。我没有创建数据库,但我获得了必要的证书和变量来创建连接。所以我从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()); 
} 

更新

我这样做时,被抛出同样的错误?

+0

你有没有试图关闭您的防火墙,试图连接? –

+0

也许[this](https://stackoverflow.com/questions/34633515/connection-made-to-google-cloud-sql-drops-intermittently)问题可能会对您有所帮助。 –

回答

0

首先,你必须检查MySQL服务器是否正在运行on.If你可以简单地

Go to Task Manager 
Go to Services 
then search for your Mysql server(eg:for my case its MYSQL56) 
then you will see under the status column it says its not running 
by right clicking and select start 

如果它已经运行,然后你要做的是在MySQL文档Windows用户,

您应该考虑在应用程序中使用之前过期和/或测试连接有效性,增加服务器配置的客户端超时值或使用Connector/J连接属性

autoReconnect的=真

避免这个问题

+0

它应该添加在哪里?在哪个文件中?为什么你建议重新连接,而连接从一开始就被拒绝? –

+0

我已更新我的答案并尝试在您的Mysql连接中使用URI mysql:// db_user:db_user @ localhost/mydb?autoReconnect = true?useUnicode = yes –

+0

仍然是同样的错误,另外我告诉过你,一个由其他人通过谷歌云创建的数据库... –

相关问题