Maven: No suitable driver found for jdbc::mysql://google

Jasper Huang :

I'm trying to connect my Java Maven project (which runs on a Jetty server) to a Google Cloud MySQL Database with the following code:

private static final String CREDENTIALS_STRING = "jdbc::mysql://google/weatherplanning?cloudSqlInstance=csci310-project-2:us-central1:myinstance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=xxxx&password=xxxx";
try {
        System.out.println("Connecting to database...");
        Class.forName("com.mysql.cj.jdbc.Driver");
        connection = DriverManager.getConnection(CREDENTIALS_STRING);
    } catch (SQLException | ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I've included both of these dependencies in my pom.xml:

    <dependency>
        <groupId>com.google.cloud.sql</groupId>
        <artifactId>mysql-socket-factory-connector-j-8</artifactId>
        <version>1.0.15</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.18</version>
    </dependency>

I'm getting

java.sql.SQLException: No suitable driver found for jdbc::mysql://google...

and I don't know what to do. I assume the dependencies should handle adding the JDBC driver to my classpath.

kurtisvg :

Your problem looks like you have a typo in your JDBC url. Your URL should start with jdbc:mysql instead of jdbc::mysql.

Additionally, you should always use a connection pool (like Hikari). Connection pools have many benefits, including better error handling, exponential backoff, and reduced latency.

You can view Hikari being used with the Cloud SQL JDBC socket factory in the context of a web application in this sample. Full instructions for deploying it are in the sample's README.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=355853&siteId=1