0
votes

I am trying to insert data into Google Cloud mysql Database through my application which is build on java through eclipse.Here is the code:

package com.example.dao;
import java.sql.Connection;
import java.sql.DriverManager;

public class dao {
    private static Connection con;
    public static Connection getcon(){
        try{
            String instanceConnectionName = "edu-vitae1";
            String databaseName = "eduvitae";
             String username = "root";
             String password = "edu";
             String jdbcUrl = String.format(
                     "jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root&useSSL=false",
                        databaseName,
                        instanceConnectionName);

                    Connection con = DriverManager.getConnection(jdbcUrl, username, password);
            Class.forName("com.mysql.jdbc.GoogleDriver");
            //con=DriverManager.getConnection("jdbc:google:mysql://edu-vitae1>/edu-vitae?user=root&password=root");
        }
        catch (Exception e) {
            System.out.println(e);
        }
        return con;
    }

}

After deploying the project,I am getting this error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root&useSSL=false

Can anyone help me to resolve this issue. Thanking You in advance

1
Post code not images of code - Joakim Danielson
@Robert Thank You for your suggestion - user10136991
@JoakimDanielson Thank You for your suggestion. - user10136991
Are you using the correct version of Cloud SQL Socket Factory for your JDBC version? See the readme for SocketFactory here and take a look at the section 'Add library as a dependency' - Christopher P
Thank You @ChristopherP for commenting but i solved the problem.The problem was very silly.Please read my answer below to know what the problem was. - user10136991

1 Answers

1
votes

After 3 days,I identified a silly mistake in my code.Notice that in my code,I have first established the connection and than loaded the driver using Class.forName("com.mysql.jdbc.GoogleDriver"); which is incorrect.You should first load the driver using Class.forName and than establish the connection using DriverManager.getConnection.Please don't make such silly mistakes like I did as I wasted 3 days on this silly error.I am posting the correct code below for your reference:

 package com.example.dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class dao {
    private static Connection con;
    public static Connection getcon(){
        try{
            String instanceConnectionName = "edu-vitae1";
            String databaseName = "eduvitae";
             String username = "root";
             String password = "root";
             Class.forName("com.mysql.jdbc.Driver");
             String jdbcUrl = String.format(
                     "jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root&useSSL=false",
                  databaseName,
                   instanceConnectionName);
                    Connection con = DriverManager.getConnection(jdbcUrl, username, password);
             //con=DriverManager.getConnection("jdbc:mysql://35.200.134.221/edu","root","root");
        }
        catch (Exception e) {
            System.out.println(e);
        }
        return con;
    }

}