Search code examples
javagoogle-app-enginegwtjdbcrequestfactory

Need help to connect to google Cloud SQL using Eclipse and GWT App Engine


I'm trying since days to connect my GWT Project to a google cloud SQL Database. I am using jdbc and when I run this class as a Java class on a local DB or on my cloud SQL db it works perfect.

package com.google.gwt.timecalc.server;

import java.io.IOException;
import java.sql.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.rdbms.AppEngineDriver;
import java.sql.*;
import com.google.cloud.sql.Driver;
public class ConnectionDB1 {

  static Connection con = null;
  static Statement stmt = null;
  static ResultSet rs = null;

  public static void main(String args[]) throws SQLException{

    DriverManager.registerDriver(new com.google.cloud.sql.Driver());
    try{
      // works local
      con = DriverManager.getConnection("jdbc:mysql://localhost/filme","root","admin" );                

      stmt = con.createStatement();

      rs = stmt.executeQuery("SELECT * FROM test");


      while ( rs.next() ){
        System.out.println("Id:                "+rs.getString(1));
        System.out.println("Name:           "+rs.getString(2));
      }

      rs.close();
      stmt.close();
      con.close();
    }

    catch ( SQLException e ){
      e.printStackTrace();
      System.exit(1);
    }   

  }
}

When I try to do it with my GWT Application it wont work. I work with Requestfactory btw. I cannot even get the Connection to the cloud SQL DB like shown here.

Thats what I get the same error like here

I worked through the google tutorial to create a new Web App with cloud sql. I also saw this but it didn't solve my problem. I also checked the google tutorials and I saw the example here: https://developers.google.com/appengine/docs/java/gettingstarted/creating Today I tried the connection turning off my firewall and antivirus but it didn't work.

Any help on this would be really appreciated. Thanks in advance.


Solution

  • After being new to this myself, and struggling with the process, I would recommend following the process here: (https://developers.google.com/appengine/docs/java/cloud-sql/developers-guide)

    It specifically was how I was able to connect, and goes through the process of registering the JDBC driver and then moving on.

    I spent hours trying to implement a slightly modified version of the initial python hello world and guestbook. I cannot recommend enough just doing exactly as they suggest with identical file names where possible.

    Hope it helps.