无法通过端点插入云端Google SQL

时间:2016-07-29 05:21:33

标签: maven google-app-engine google-cloud-sql

我创建了一个Maven Endpoint项目,我想在云上将我的项目与google SQL连接起来。我已经在云上创建了一个数据库,我试图通过我的Maven Endpoint项目插入到该数据库中,但是当我执行我的Endpoint Project时,我无法插入到数据库中。我是第一次使用端点,因此我无法弄清楚我哪里出错了。非常感谢。

提前谢谢你。

下面的代码是我的Endpoint项目代码用于在云上连接和插入谷歌SQL。

package com.syncing.SuperMarket;  

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import javax.inject.Named;
import java.sql.DriverManager; 
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Api(
name = "supermarket",
version = "v1",
scopes = {Constants.EMAIL_SCOPE},
clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID,Constants.IOS_CLIENT_ID},
audiences = {Constants.ANDROID_AUDIENCE}
)
public class Sync {
  /**
   * Insert a row into cloud Google SQL Database
   * @param no_of_products
   * @param total_amt
   * @param u_id
   * @return
   */ 
      @ApiMethod(name = "insertData")
      public void getPurchaseDetails(@Named("no_of_products") int no_of_products,@Named("total_amt") int total_amt,@Named("u_id") int u_id)
      {
              try {
                  Class.forName("com.mysql.jdbc.GoogleDriver");
                  java.sql.Connection conn = DriverManager.getConnection("jdbc:google:mysql://app-id:instance-id/db_name?user=username&password=password");
                  String query = "INSERT INTO purchase (no_of_products,total_amt,u_id) VALUES (?,?,?)";
                  CallableStatement stmt = conn.prepareCall(query);
                  stmt.setInt(1, no_of_products);
                  stmt.setInt(2, total_amt);
                  stmt.setInt(3, u_id);
                  stmt.execute();
                  stmt.close();
                  conn.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }      
      }

}

0 个答案:

没有答案