The Google Plugin for Eclipse is deprecated and will be removed in January 2018. Migrate to Cloud Tools for Eclipse and/or the GWT Eclipse Plugin as soon as possible to avoid disruption.

Creating a New Web App with Cloud SQL Support

After performing the required Cloud SQL tasks to prepare for your web application, you are ready to use the App Engine wizard to create a new project in Eclipse that you can use to connect to a Google Cloud SQL instance.


Creating a web application project

  1. In Eclipse, select File > New > Web Application Project and provide a Project name, Package name, and an optional Project ID.

    For more detailed instructions, see Creating a New Web Application.

  2. Use the default version of the Google App Engine SDK if you don't have a reason to use an older version.

  3. Click Finish.
  4. Add the MySQL Connector/J JAR to the project.

    This library is used when you are connecting from your web application to a development MySQL instance, that is when you are using the The Java Development Server.

    If you downloaded a compressed MySQL Connector/J archive file, extract it and make sure the connector Java JAR is on the build path for the project you just created. A simple way do this by copying the JAR to the /WEB-INF/lib folder of the project. JARs in the WEB-INF/lib folder are automatically included in the classpath for the web application. See the Running your application locally section for more information about to create a launch configuration that does not require putting the JAR into the /WEB-INF/lib folder.

    An example project Guestbook is shown below with the MySQL Connector/J JAR added to war/WEB-INF/lib folder in the project.

Creating a general coding pattern for use in local and production environments

You can connect to your Cloud SQL instance from both Java code and Java Server Pages (JSP). Regardless of the code used to connect, you should follow a general pattern that uses the environment (development or production) to set up a connection. The pattern has the following behavior:

  • If the application is running in development, such as locally, then use a JDBC database URL of the format jdbc:mysql://<your-database-name>?user=root to connect to a local database instance. This JDBC database URL will be handled by the MySQL JDBC driver com.mysql.jdbc.Driver.
  • If the web application is in production (i.e., the application has been deployed), then use a JDBC database URL of the format jdbc:google:mysql://<your-project-id>:<your-instance-name>/<your-database-name>?user=root to connect to your Cloud SQL instance. This JDBC database URL will be handled by the Google App Engine-specific MySQL JDBC driver com.mysql.jdbc.GoogleDriver.

The following code snippet shows the pattern for Java. You must provide values for <your-project-id>, <your-instance-name>, and <your-database-name> in the code.

if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
  url = "jdbc:google:mysql://<your-project-id>:<your-instance-name>/<your-database-name>?user=root";
} else {
  // Local MySQL instance to use during development.
  url = "jdbc:mysql://<your-database-name>?user=root";

The snippet can throw checked exceptions that must be declared in a throws clause of the surrounding method, or handled in a surrounding try-catch block.

The following code snippet shows the pattern for JSP.

String url = null;
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
  url = "jdbc:google:mysql://<your-project-id>:<your-instance-name>/<your-database-name>?user=root";
} else {
  // Local MySQL instance to use during development.
  url = "jdbc:mysql://<your-database-name>?user=root";

For an example that demonstrates the pattern in both Java and JSP code, see Using Google Cloud SQL with App Engine Java SDK.

Running your application locally

When you finish coding your web application, you can run your app locally in the dev server to test it.

In the Creating a web application project for Google Cloud SQL section, you added the MySQL Connector/J library for development use. When deploying, the library isn't needed because it is automatically available for use by deployed Google App Engine applications. Therefore, when your application is ready to be deployed, you can remove the Connector/J library from the war/WEB-INF/lib folder. It does not hurt to keep it, but it isn't used.

Alternatively, if you just want to use the MySQL Connector/J library for development, but not deploy it, then you can create a custom development launch configuration that links to the library. The example below shows a launch configuration that adds the MySQL Connector/J library to the classpath.

If you run your application locally and receive the error "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver", make sure the MySQL Connector/J JAR is either in the war/WEB-INF/lib folder of your project or on the classpath of the run configuration you are using.

Deploying your application

When you finish testing your web application locally, deploy your web app to App Engine. The App Engine ID you use to deploy must be one that has been granted permission to connect to the Cloud SQL instance used in the web application. To grant permission to an App Engine application, see Configuring access control for App Engine applications.

Send feedback about...

Google Plugin for Eclipse