gpt4 book ai didi

java - 无法使用 Eclipse 连接到 Google Cloud SQL

转载 作者:行者123 更新时间:2023-11-30 10:22:57 25 4
gpt4 key购买 nike

我在从 eclipse 连接到我的数据库时遇到问题。 mysql java 连接器和 mysql 套接字工厂连接器 jar 文件包含在构建路径中。我不确定是什么导致了 NoClassDefFound 错误。凭据正确且在代码中被省略。

我的代码:

import java.sql.*;

public class TestConnect {

private static String instanceConnectionName = "sampleInstanceName";
private static String databaseName = "sampleDatabaseName";
private static Connection connection = null;
private static String userName="sampleUserName";
private static String password="samplePassword";
private static String dbURL = String.format("jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
databaseName, instanceConnectionName);

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
CreateConnection();
}

public static void CreateConnection() {
try {
// connect
connection = DriverManager.getConnection(dbURL,userName, password);
} catch (SQLException ex) {
System.out.println("Connection Failed! Check output console");
ex.printStackTrace();
}
}

}

这是 pom.xml:

  <dependencies>
<!-- https://mvnrepository.com/artifact/com.google.cloud.sql/mysql-socket-factory -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.4</version>
</dependency>
<dependency> <!-- http://dev.mysql.com/doc/connector-j/en/ -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>

</dependencies>
</project>

这是错误信息:

java.sql.SQLException: java.lang.NoClassDefFoundError: com/mysql/cj/api/io/SocketFactory
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:877)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:873)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:443)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TestConnect.CreateConnection(TestConnect.java:32)
at TestConnect.main(TestConnect.java:26)
Caused by: java.lang.NoClassDefFoundError: com/mysql/cj/api/io/SocketFactory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.mysql.jdbc.MysqlIO.createSocketFactory(MysqlIO.java:3328)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:296)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2222)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2017)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
... 6 more
Caused by: java.lang.ClassNotFoundException: com.mysql.cj.api.io.SocketFactory
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 32 more

更新

我更新了 jar 文件并删除了不需要的文件,并相应地更新了 pom 文件。

这是新的 pom 文件:

  <dependencies>
<!-- https://mvnrepository.com/artifact/com.google.cloud.sql/mysql-socket-factory-connector-j-6 -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory-connector-j-6</artifactId>
<version>1.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.cloud.sql/jdbc-socket-factory-core -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>jdbc-socket-factory-core</artifactId>
<version>1.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>

这是新的错误信息:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
at com.google.cloud.sql.mysql.SocketFactory.connect(SocketFactory.java:41)
at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:57)
at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1726)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1596)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:633)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:347)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:219)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TestConnect.CreateConnection(TestConnect.java:30)
at TestConnect.main(TestConnect.java:24)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Preconditions
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 12 more

最佳答案

com.mysql.cj.api.io.SocketFactory 是 MySQL Connector/J 版本 6 的一部分。它由 mysql-socket-factory-connector 中的 SocketFactory 使用-j-6。但是,您共享的 pom.xml 使用 mysql-socket-factory,这是版本 5 数据包,不应使用它。

是否有可能您之前使用过 mysql-socket-factory-connector-j-6 并且它在您的库中?这可能会导致错误加载 v6 SocketFactory,从而导致此问题。如果是这样,请尝试 mvn clean 删除旧的 jar 文件并尝试再次运行。

关于java - 无法使用 Eclipse 连接到 Google Cloud SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47296318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com