简体   繁体   English

在Eclipse中进行开发时无法从Google App Engine连接到Google Cloud SQL实例

[英]Unable to connect to Google Cloud SQL instance from Google App Engine when developing in Eclipse

EDIT: This has been resolved by checking the log file on the Google Developers Console. 编辑:通过检查Google Developers Console上的日志文件已解决了该问题。 The error was not in connecting to the database, but was because I was compiling with JDK 1.8 instead of 1.7. 该错误不是在连接数据库时出现的,而是因为我使用的是JDK 1.8而不是1.7。 It turned out that even though I had told Eclipse to use JDK 1.7 for the project, it was still using 1.8 because I had the following line in my Eclipse configuration file: 事实证明,即使我已告诉Eclipse在项目中使用JDK 1.7,但仍在使用1.8,因为我在Eclipse配置文件中包含以下内容:

-vm
C:\Program Files\Java\jdk1.8.0_20\bin

when I changed this to: 当我将其更改为:

-vm
C:\Program Files\Java\jdk1.7.0_71\bin

everything worked perfectly. 一切正常。

Original Question: 原始问题:

I am trying to connect to a Google Cloud SQL instance from my authorized Google App Engine application. 我正在尝试从授权的Google App Engine应用程序连接到Google Cloud SQL实例。 I started by following the directions at https://cloud.google.com/appengine/docs/java/cloud-sql/#Java_Build_a_starter_application_and_database and modifying them slightly to work with my Cloud SQL instance (I did not implement the guestbook database, I used my own which also has just one table with three columns). 我首先按照https://cloud.google.com/appengine/docs/java/cloud-sql/#Java_Build_a_starter_application_and_database上的说明进行了操作,并对其进行了少许修改以与我的Cloud SQL实例配合使用(我未实现访客留言板数据库,使用了我自己的表格,该表格也只有一个表格,包含三列)。 I created the Cloud SQL instance using MySQL Workbench. 我使用MySQL Workbench创建了Cloud SQL实例。

When I debug my application locally (run it at localhost:8888) it works perfectly with my local MySQL instance AND the Google Cloud SQL instance. 当我在本地调试应用程序(在localhost:8888上运行)时,它可以与本地MySQL实例和Google Cloud SQL实例完美配合 However, when I try to deploy my application to Google App Engine it just spits back a 500 Server Error and I have no clue what went wrong (I am very new to web programming). 但是,当我尝试将应用程序部署到Google App Engine时,它只会返回500服务器错误,并且我不知道出了什么问题(我对Web编程非常陌生)。 I have been searching all over the web for a solution to this issue but I've yet to find something that does the trick. 我一直在网上搜索该问题的解决方案,但还没有找到解决问题的办法。

Here is my code for the servlet: 这是我的servlet代码:

package com.rmpm;

import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import com.google.appengine.api.utils.SystemProperty;


@SuppressWarnings("serial")
public class Synaptic_rmpmServlet extends HttpServlet {
    @Override
      public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String url = null;
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        try {
          if (SystemProperty.environment.value() ==
              SystemProperty.Environment.Value.Production) {
            // Load the class that provides the new "jdbc:google:mysql://" prefix.
            Class.forName("com.mysql.jdbc.GoogleDriver");
            url = "jdbc:google:mysql://<my-project-id>:<my-instance-id>/test"; // Doesn't work
          } else {
            // Local MySQL instance to use during development.
            Class.forName("com.mysql.jdbc.Driver");
            url = "jdbc:mysql://173.###.##.###:3306/test"; // Connects to Google Cloud SQL instance when root password is provided
            //url = "jdbc:mysql://127.0.0.1:3306/test";    // Connects to local MySQL instance
          }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        try {
          Connection conn = DriverManager.getConnection(url, "root", "");
          try {
            String pid = req.getParameter("projID");
            String own = req.getParameter("owner");
            String dur = req.getParameter("duration");
            if (pid == "" || own == "" || dur == "") {
              out.println(
                  "<html><head></head><body>You are missing either a projectID, owner name, or duration! Try again! " +
                  "Redirecting in 3 seconds...</body></html>");
            } 
            else {
                int duration = Integer.parseInt(dur);
                String statement = "INSERT INTO project VALUES(?, ?, ?)";
                PreparedStatement stmt = conn.prepareStatement(statement);
                stmt.setString(1, pid);
                stmt.setString(2, own);
                stmt.setInt(3, duration);
                int success = 2;
                success = stmt.executeUpdate();
                if (success == 1) {
                    out.println(
                            "<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");
                } else if (success == 0) {
                    out.println(
                            "<html><head></head><body>Failure! Please try again! " +
                            "Redirecting in 3 seconds...</body></html>");
                }
            }
          } finally {
           conn.close();
          }
        } 
        catch (SQLException e) {
            e.printStackTrace();
        }
        resp.setHeader("Refresh", "3; url=/databaseIO.jsp");
      }
}

And my code for the databaseIO.jsp: 而我的数据库IO.jsp代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.google.appengine.api.utils.SystemProperty" %>

<html>
  <body>

<%
String url = null;
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
  // Load the class that provides the new "jdbc:google:mysql://" prefix.
  Class.forName("com.mysql.jdbc.GoogleDriver");
  url = "jdbc:google:mysql://<my-project-id>:<my-instance-id>/test";   // Doesn't work
} else {
  // Local MySQL instance to use during development.
  Class.forName("com.mysql.jdbc.Driver");
  url = "jdbc:mysql://173.###.##.###:3306/test";       // Connects to Google Cloud SQL instance when root password is provided
  //url = "jdbc:mysql://127.0.0.1:3306/test";          // Connects to local MySQL instance
}

Connection conn = DriverManager.getConnection(url, "root", "");
ResultSet rs = conn.createStatement().executeQuery(
    "SELECT * FROM project");
%>

<table style="border: 1px solid black">
<tbody>
<tr>
<th width="40%" style="background-color: #CCFFCC; margin: 5px">projectID</th>
<th style="background-color: #CCFFCC; margin: 5px">owner</th>
<th style="background-color: #CCFFCC; margin: 5px">duration</th>
</tr>

<%
while (rs.next()) {
    String aPID = rs.getString(1);
    String anOwner = rs.getString(2);
    int aDur = rs.getInt(3);
 %>
<tr>
<td><%= aPID %></td>
<td><%= anOwner %></td>
<td><%= aDur %></td>
</tr>
<%
}
conn.close();
%>

</tbody>
</table>
<br />
No more projects!
<p><strong>Add a project:</strong></p>
<form action="/sign" method="post">
    <div>Project ID: <input type="text" name="projID"></input></div>
    <br />
    <div>Owner: <input type="text" name="owner"></input></div>
    <br />
    <div>Duration: <input type="text" name="duration"></input></div>
    <br />
    <div><input type="submit" value="Insert Project"></input></div>
  </form>
  </body>
 </html>

Based on what I've read online, I gather that I shouldn't need a password when connecting as the root user to my Google Cloud SQL instance from my authorized Google App Engine application. 根据我在网上阅读的内容,我发现以root用户身份从授权的 Google App Engine应用程序连接到我的Google Cloud SQL实例时,不需要密码。 When I connect to the Google Cloud SQL instance locally in debug mode, I do provide the root password which I set in the Google Developers Console and it works perfectly. 当我以调试模式在本地连接到Google Cloud SQL实例时,我确实提供了我在Google Developers Console中设置的root密码,它可以正常工作。

What I've done: 我所做的:

  • I am already logged into Eclipse with the Google account that is linked to my Google App Engine application. 我已经使用链接到我的Google App Engine应用程序的Google帐户登录了Eclipse。
  • I have already included the <use-google-connector-j>true</use-google-connector-j> line in the appengine-web.xml file. 我已经在appengine-web.xml文件中加入了<use-google-connector-j>true</use-google-connector-j>行。
  • I have copied the mysql-connector-java-5.1.33-bin.jar to the project's war/WEB-INF/lib folder and to the GAE SDK directory at D:\\Program_Files\\Eclipse\\plugins\\com.google.appengine.eclipse.sdkbundle_1.9.13\\appengine-java-sdk-1.9.13\\lib\\impl (reading similar questions online said to do this). 我已将mysql-connector-java-5.1.33-bin.jar复制到项目的war / WEB-INF / lib文件夹,并复制到位于D:\\ Program_Files \\ Eclipse \\ plugins \\ com.google.appengine的GAE SDK目录。 eclipse.sdkbundle_1.9.13 \\ appengine-java-sdk-1.9.13 \\ lib \\ impl(在线阅读类似的问题可以做到这一点)。
  • I have authorized my Google App Engine application to connect to my Google Cloud SQL instance. 我已授权我的Google App Engine应用程序连接到我的Google Cloud SQL实例。
  • I have authorized my local network to connect to my Google Cloud SQL instance. 我已授权我的局域网连接到我的Google Cloud SQL实例。
  • I am 100% sure the <my-project-id> and <my-instance-id> fields are correct in the JDBC url. 我100%确定JDBC URL中的<my-project-id><my-instance-id>字段正确。
  • I've tried using my Google Cloud SQL instance's IP address in the JDBC url instead of the project-id and instance-id. 我尝试在JDBC网址中使用Google Cloud SQL实例的IP地址,而不是project-id和instance-id。

What I'm using: 我正在使用什么:

  • Eclipse SE Luna (4.4.1) with Google Plugin for Eclipse (4.4) and Google App Engine Java SDK (1.9.13). Eclipse SE Luna(4.4.1),以及适用于Eclipse的Google插件(4.4)和Google App Engine Java SDK(1.9.13)。 I deploy from Eclipse straight to Google App Engine with the plugin. 我使用插件从Eclipse直接部署到Google App Engine。
  • JDK 1.7 JDK 1.7
  • Windows 8.1 64-bit Windows 8.1 64位

If you need any additional information just post here as I'll be checking this regularly. 如果您需要任何其他信息,请在此处发布,因为我会定期检查。

本地mysql将在您的代码上工作,我遇到了同样的问题,但是随着我将.GoogleDriver更改为.Driver,它开始起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Google App Engine Eclipse插件连接到Cloud SQL - Connect Google App Engine Eclipse Plugin to Cloud SQL Google APP Engine 和云 sql :: 无法在 Google 云 sql(我的 sql)中连接 Spring boot 应用程序 - Google APP Engine and cloud sql :: Unable to connect Spring boot app in Google cloud sql (my sql) 使用Google App Engine端点连接到Cloud SQL - Connect to Cloud SQL with Google App Engine Endpoint Spring 启动应用程序在部署到 Google App Engine 时无法连接到 Google Cloud SQL (PostgreSQL) - Spring Boot app can't connect to Google Cloud SQL (PostgreSQL) when deploying to Google App Engine 谷歌应用程序引擎:切换sql云数据库实例,保持源代码与java eclipse相同 - google app engine : switch sql cloud database instance , keeping the source code same java eclipse 部署后的Google App Engine和Cloud SQL - Google App Engine and Cloud SQL when deployed 无法从Google App Engine(春​​季启动应用)连接到Google Cloud SQL(第二代) - Can not connect to google cloud sql(second generation) from google app engine(spring boot app) 需要帮助以使用Eclipse和GWT App Engine连接到Google Cloud SQL - Need help to connect to google Cloud SQL using Eclipse and GWT App Engine 没有应用引擎,如何连接Google Cloud实例 - How could i connect with Google cloud instance without app engine 无法从 Eclipse 中的本地 App Engine 实例连接到 BigQuery - Unable to Connect to BigQuery from local App Engine instance in Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM