17. Before Connect
● CloudSQL instance ready
● Authorized
○ root password
○ instance ip address
○ access ip address
18. Configure Connectors
● Hostname is Cloud SQL instance IP address
● Connection URL includes instance IP and
database name
● Java connection is:
jdbc:mysql://<instance-ip>:3306/
19. Local Test Configure
appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>mitac-cp300</application>
<version>eord</version>
….
<use-google-connector-j>true</use-google-connector-j>
….
</appengine-web-app>
20. Connect from App Engine
● JDBC
● Java Persistence API (JPA)
● Java Data Objects (JDO)
● Other JPAs custom additions: Hibernate, EclipseLink
jdbc:google:mysql://...
(instead of jdbc:mysql://...)
21. Detect running environment
Connection conn = null;
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
// app is running in production
Class.forName("com.mysql.jdbc.GoogleDriver");
String url= "jdbc:google:mysql://instance-name/";
conn = DriverManager.getConnection(url);
...
} else {
// app is running on localhost
Class.forName("com.mysql.jdbc.Driver");
String url= "jdbc:mysql://db-ip-address/";
conn = DriverManager.getConnection(url);
...
}
conn.~
22. DriverManager.registerDriver(new AppEngineDriver());
conn = DriverManager.getConnection(connUrl);
String sql = "SELECT count(*) as total FROM " + tableName + "";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet results = stmt.executeQuery();
while (results.next()) {
int total = results.getInt("total");
…
}
Query Example - Standard JDBC