Ich möchte eine App in Google App Engine bereitstellen und dem Benutzer den Zugriff über POST aus dem Internet ermöglichen. Wie ich weiß, dass com.mysql.jdbc.GoogleDriver
nur funktionieren, während die App bei GAE bereitgestellt wird. Also, ich versuche es jedes Mal nach der Bereitstellung auf der GAE. Leider erhalte ich immer den Fehler Teil der Antwort. Ich denke, das ist das Problem mit meiner Verbindung mit Google Cloud SQL.Google SQL funktioniert nicht in Google App Engine
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<!--
Allows App Engine to send multiple requests to one instance in parallel:
-->
<threadsafe>true</threadsafe>
<use-google-connector-j>true</use-google-connector-j>
<system-properties>
<property name="ae-cloudsql.cloudsql-database-url" value="jdbc:google:mysql://${INSTANCE_CONNECTION_NAME}/${database}?user=${user}&password=${password}" />
</system-properties>
<!--
HTTP Sessions are disabled by default. To enable HTTP sessions specify:
<sessions-enabled>true</sessions-enabled>
It's possible to reduce request latency by configuring your application to
asynchronously write HTTP session data to the datastore:
<async-session-persistence enabled="true" />
With this feature enabled, there is a very small chance your app will see
stale session data. For details, see
https://cloud.google.com/appengine/docs/java/config/appconfig#Java_appengine_web_xml_Enabling_sessions
-->
</appengine-web-app>
Web.xml
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@POST
@Path("/v3")
public Response getSAppData(AppDataRequest adr) {
Response data = ads.getSAppData(adr.getId(), adr.getEmail(), adr.getPassword());
return data;
}
POST Code
public class AppDataService {
Map<AppDataRequest, Data> DataHM = new HashMap<>();
static final String JDBC_DRIVER = "com.mysql.jdbc.GoogleDriver";
static final String DB_URL = "jdbc:google:mysql://****:****/****?user=root";
static final String USER = "****";
static final String PASS = "****";
public AppDataService(){
Connection conn = null;
Statement stat = null;
try{
Class.forName("com.mysql.jdbc.GoogleDriver");
conn = DriverManager.getConnection(DB_URL);
stat = conn.createStatement();
String sql = "SELECT * FROM testdata";
ResultSet resu = stat.executeQuery(sql);
while(resu.next()){
int id = resu.getInt("app_id");
String email = resu.getString("email");
String password = resu.getString("password");
String token = resu.getString("token");
DataHM.put(new AppDataRequest(id, email, password), new Data(token));
}
resu.close();
stat.close();
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(stat!=null){
stat.close();
}
}
catch (SQLException se2){
se2.printStackTrace();
}
try{
if(conn!=null){
conn.close();
}
}
catch(SQLException se3){
se3.printStackTrace();
}
}
}
public Response getSAppData(int id, String email, String password){
Map<String, AppData> AppDataHM = new HashMap<>();
Map<String, Data> DataHM1 = new HashMap<>();
Map<String, List<String>> DataHM2 = new HashMap<>();
HashMap<Object, Object> ADHMDHM = new HashMap<>();
List<String> message = new ArrayList<>();
Data data = DataHM.get(new AppDataRequest (id, email, password));
List<String> data2 = new ArrayList<>();
if(data != null){
message.add("");
AppDataHM.put("AppData", new AppData("success", message));
DataHM1.put("Data", data);
ADHMDHM.putAll(AppDataHM);
ADHMDHM.putAll(DataHM1);
String ADHMDHM1 = new Gson().toJson(ADHMDHM);
return Response.status(200).entity(ADHMDHM1).build();
}
else{
message.add("Your login information is invalid. Please try with the correct information");
AppDataHM.put("AppData", new AppData("error", message));
DataHM2.put("Data", data2);
ADHMDHM.putAll(AppDataHM);
ADHMDHM.putAll(DataHM2);
String ADHMDHM2 = new Gson().toJson(ADHMDHM);
return Response.status(200).entity(ADHMDHM2).build();
}
}
}
-Code für den Zugriff auf Google SQL
GAE Mit Postman
Vielen Dank im Voraus für alle Benutzer, die diese Frage beantworten helfen.
Vielleicht müssen Sie 'DriverManager.getConnection (String url, String Benutzer, String password)' stattdessen verwenden. Sie liefern derzeit nur einen Benutzernamen in der URL (der etwas seltsam ist) und kein Passwort. –
Hallo. @MarkRotteveel Bevor ich meine App in der Google App Engine bereitstellen kann, wird 'DriverManager.getConnection (String URL, String Benutzer, String Passwort)' funktionieren, aber es funktioniert nicht nach der Bereitstellung und Sie können [hier] verweisen (http://stackoverflow.com/a/20173117/7772730) für weitere Informationen. Ich liefere nur einen Benutzernamen, weil ich kein Passwort festlege und nur den root-Account benutze. –