簡體   English   中英

“無法創建與數據庫服務器的連接” netbeans谷歌雲sql錯誤

[英]“Could not create connection to database server” netbeans google cloud sql error

我正在嘗試使用java httpServlet和netbeans創建我的第一個API,這些API將根據其示例文檔連接到Google Cloud上的數據庫。 我尚未創建數據庫,但已獲得創建連接所需的憑據和變量。 因此,我從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", 
"", 
"");

有什么提示嗎?

首先,您必須檢查mysql服務器是否正在運行。如果您是Windows用戶,則只需

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文檔中的步驟進行操作,

您應該考慮在應用程序中使用連接之前使連接有效性到期和/或對其進行測試,或者增加服務器為客戶端超時配置的值,或者使用Connector / J連接屬性

autoReconnect的=真

避免這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM