Llevo unos días intentando conectar cloud SQL desde app engine con java. pero al hacer la conexión me da el error:
No suitable driver found for jdbc:mysql://104.155.83.62/BD-myApp?user=root;password=root
Os dejo el selvlet para conectar:
@SuppressWarnings("serial")
public class CloudSqlServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
ServletException {
String path = req.getRequestURI();
if (path.startsWith("/favicon.ico")) {
return; // ignore the request for favicon.ico
}
// store only the first two octets of a users ip address
String userIp = req.getRemoteAddr();
InetAddress address = InetAddress.getByName(userIp);
if (address instanceof Inet6Address) {
// nest indexOf calls to find the second occurrence of a character in a string
// an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()
userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*";
} else if (address instanceof Inet4Address) {
userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*";
}
final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL "
+ "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, nombre VARCHAR(46) NOT NULL)";
final String selectSql = "SELECT * from visits;";
PrintWriter out = resp.getWriter();
resp.setContentType("text/plain");
String url;
if (System.getProperty("com.google.appengine.runtime.version").startsWith("Google App Engine/")) {
// Check the System properties to determine if we are running on appengine or not
// Google App Engine sets a few system properties that will reliably be present on a remote
// instance.
url = System.getProperty("ae-cloudsql.local-database-url");
try {
System.out.println("Entra en appengine cloud");
// Load the class that provides the new "jdbc:google:mysql://" prefix.
Class.forName("com.mysql.jdbc.GoogleDriver");
} catch (ClassNotFoundException e) {
throw new ServletException("Error loading Google JDBC Driver", e);
}
} else {
System.out.println("NO entra en appengine cloud");
// Set the url with the local MySQL database connection url when running locally
url = System.getProperty("ae-cloudsql.local-database-url");
}
try (Connection conn = DriverManager.getConnection(url)){
try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) {
out.print("Datos:");
while (rs.next()) {
String savedIp = rs.getString("user_ip");
String name = rs.getString("nombre");
out.print("IP: " + name + " Nombre: " + savedIp);
}
}
} catch (SQLException e) {
throw new ServletException("SQL error", e);
}
}
}
==========================================================================
Y el appengine-web.xml:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>myApp</application>
<version>1</version>
<threadsafe>true</threadsafe>
<use-google-connector-j>true</use-google-connector-j>
<!-- Configure java.util.logging -->
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<system-properties>
<property name="ae-cloudsql.cloudsql-database-url" value="jdbc:google:mysql://project-id-4811673067964427457:europe-west1:con-bd-myApp/BD-myApp?user=root;password=root"/>
<property name="ae-cloudsql.local-database-url" value="jdbc:mysql://104.155.83.62/BD-myApp?user=root;password=root"/>
</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
http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions
-->
</appengine-web-app>