Google SQL 无法在 Google App Engine 上工作

Google SQL Not Working On Google App Engine

我希望将应用程序部署到 Google App Engine 并允许用户使用 POST 从 Web 访问它。据我所知,com.mysql.jdbc.GoogleDriver 仅在应用程序部署在 GAE 期间有效。所以,每次部署到 GAE 后,我都会尝试它。不幸的是,我总是收到回复的错误部分。我认为这是我与 Google Cloud SQL.

连接的问题
<?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}&amp;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代码

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();
        }   
    }
}

访问代码GoogleSQL

JAR 文件

GAE Return 使用 POSTman

提前感谢所有帮助回答此问题的用户。

鲁本。我觉得您的代码无法正常工作,因为您使用的是 Google 云 SQL URL 的实例 ID 而不是实例连接名称。您的 Web.XML 表示您应该使用 <property name="ae-cloudsql.cloudsql-database-url" value="jdbc:google:mysql://${INSTANCE_CONNECTION_NAME}/${database}?user=${user}&amp;password=${password}" />

中的 ${INSTANCE_CONNECTION_NAME}

你可以查看这个Setting up the Cloud SQL instance