编辑:已通过检查Google Developers Console上的日志文件解决了此问题。错误不是连接到数据库,而是因为我使用JDK 1.8而不是1.7进行编译。事实证明,即使我告诉Eclipse使用JDK 1.7作为项目,它仍然使用1.8,因为我在Eclipse配置文件中有以下行:
-vm
C:\Program Files\Java\jdk1.8.0_20\bin
当我将其更改为:
-vm
C:\Program Files\Java\jdk1.7.0_71\bin
一切都运转良好。
原始问题:
我正在尝试从我的授权Google App Engine应用程序连接到Google Cloud SQL实例。我开始按照https://cloud.google.com/appengine/docs/java/cloud-sql/#Java_Build_a_starter_application_and_database上的说明进行操作,然后稍微修改它们以使用我的Cloud SQL实例(我没有实现留言簿数据库,我使用了自己的数据库,只有一个表有三列)。我使用MySQL Workbench创建了Cloud SQL实例。
当我调试我的应用程序本地(在localhost:8888上运行)时,它与我的本地MySQL实例和Google Cloud SQL实例完美配合。然而,当我尝试将我的应用程序部署到谷歌应用程序引擎它只是吐出500服务器错误,我不知道出了什么问题(我对网络编程很新)。我一直在网上搜索这个问题的解决方案,但我还没有找到能解决问题的方法。
这是我的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");
}
}
我的databaseIO.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>
根据我在线阅读的内容,我认为在从授权 Google App Engine应用程序以root用户身份连接到我的Google Cloud SQL实例时,我不需要密码。当我在调试模式下本地连接到Google Cloud SQL实例时,我确实提供了我在Google Developers Console中设置的root密码,它运行正常。
我做了什么:
<use-google-connector-j>true</use-google-connector-j>
行。<my-project-id>
和<my-instance-id>
字段是正确的。我正在使用的是什么:
如果您需要任何其他信息,请在此处发布,因为我会定期查看此信息。
答案 0 :(得分:0)
本地mysql将处理你的代码,我遇到了同样的问题但是我改变了.GoogleDriver to .Driver它开始工作