[英]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: 我所做的:
<use-google-connector-j>true</use-google-connector-j>
line in the appengine-web.xml file. <use-google-connector-j>true</use-google-connector-j>
行。 <my-project-id>
and <my-instance-id>
fields are correct in the JDBC url. <my-project-id>
和<my-instance-id>
字段正确。 What I'm using: 我正在使用什么:
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.