Search code examples
google-apigoogle-cloud-sql

Connecting to Google cloud sql from Java stand alone application


I want to write a java stand alone application to connect to google cloud sql - MySql database. I could find samples for app client but not for a stand alone java application.

When I tried to do this, I get the following error.

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:google:mysql://IP:Instance_name?user=user_name");
Statement st = con.createStatement();
ResultSet rs = con.createStatement().executeQuery("SELECT 1 + 1");

It fails with error:

No suitable driver found for jdbc:google:mysql:..

I see some answers where they have asked to enabled mysql connector. But it is the case with app engine project. How do I do it with stand alone application?


Solution

  • You can just use the standard mysql connector to connect to cloud sql instance: e.g:

    DriverManager.getConnection("jdbc:mysql://IP:Instance_name?user=user_name");
    

    You can check this link https://cloud.google.com/sql/docs/external for more information.