简体   繁体   English

Maven:找不到适合 jdbc::mysql://google 的驱动程序

[英]Maven: No suitable driver found for jdbc::mysql://google

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:我正在尝试使用以下代码将我的 Java Maven 项目(在 Jetty 服务器上运行)连接到 Google Cloud MySQL 数据库:

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:我已经在我的 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... java.sql.SQLException: 找不到适合 jdbc::mysql://google 的驱动程序...

and I don't know what to do.我不知道该怎么办。 I assume the dependencies should handle adding the JDBC driver to my classpath.我假设依赖项应该处理将 JDBC 驱动程序添加到我的类路径。

Your problem looks like you have a typo in your JDBC url.您的问题看起来像是您的 JDBC url 中有一个错字。 Your URL should start with jdbc:mysql instead of jdbc::mysql .您的 URL 应该以jdbc:mysql而不是jdbc::mysql开头。

Additionally, you should always use a connection pool (like Hikari ).此外,您应该始终使用连接池(如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 .您可以在此示例中的 Web 应用程序上下文中查看 Hikari 与 Cloud SQL JDBC 套接字工厂一起使用的情况 Full instructions for deploying it are in the sample's README . 部署它的完整说明在示例的 README 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM