问题
I have done everything that is required of me and my deployed backend servlet on the google app engine isn't making any connection to my google cloud database and the worst part of it, is that, my app engine log isn't even showing any error at all, its showing "POST /hello HTTP/1.1" 200 46... I seriously don't understand what is going on here for the past 5days..
these are the things i have done below:
This is my servlet code:
public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Please use the form to POST to this url");
}
@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) {
// Connecting from App Engine.
// Load the class that provides the new "jdbc:google:mysql://" prefix.
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://festive-shark-90408:daily-joy-project/database?user=joys";
} else {
// Local MySQL instance to use during development.
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://<cloud-sql-instance-ip>:3306/database?user=joys";
// Alternatively, connect to a Google Cloud SQL instance using:
}
} catch (Exception e) {
e.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection(url);
try {
String fname = req.getParameter("name");
// String content = req.getParameter("message");
String statement = "INSERT INTO message (tags, posts, date, datetime) VALUES( ? , 'message', now(), now() )";
PreparedStatement stmt = conn.prepareStatement(statement);
stmt.setString(1, fname);
// stmt.setString(2, content);
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();
}
}
}
In my appengine-web.xml, below are the parameters:
application value: festive-shark-90408
version value: app-001
threadsafe value: true
use-google-connector-j value: true
system-properties:
property name="java.util.logging.config.file" value="WEB-INF/logging.properties"
My android studio is using the JDK 1.7.0_72
My appengine sdk is appengine-java-sdk-1.9.18
I have set the Access control in my cloud sql to authorize my app engine application and also to authorize my network ip.
I already deployed my backend servlet successfully, whenever I make a post request to the servlet, my app engine log shows ONLY this:
203.106.176.214 - - [04/Apr/2015:04:43:17 -0700] "POST /hello HTTP/1.1" 200 45 - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36" "festive-shark-90408.appspot.com" ms=39 cpu_ms=76 cpm_usd=0.000005 instance=00c61b117c0b660d29c1e893219b346d423915 app_engine_release=1.9.18
Meaning it was successfully because status code 200..
My cloud sql database isn't being queried at all after this, but whenever I run the server locally on my laptop, everything works fine and my cloud sql gets the connection.
It just doesn't work with my app engine.
WHY? I really don't understand where the problem is coming from.
回答1:
I figured out what was wrong..
First, I was meant to use root as my username when connecting to the cloud sql database from the app engine.
Secondly, I was meant to change the App Engine Project Title from its default name to a name i want to give it.. (I still don't know why this is affecting it)
来源:https://stackoverflow.com/questions/29446212/i-cant-connect-to-my-google-cloud-sql-database-from-my-deployed-backend-servlet