我无法从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
答案 0 :(得分:0)
com.mysql.cj.api.io.SocketFactory
是MySQL Connector / J版本6的一部分。这由mysql-socket-factory-connector-j-6
中的SocketFactory使用。但是,您共享的pom.xml使用的是mysql-socket-factory
,即版本5数据包,不应使用它。
以前是否有可能使用mysql-socket-factory-connector-j-6
并且它在您的库中?这可能允许错误地加载v6 SocketFactory,从而导致此问题。如果是这样,请尝试mvn clean
删除旧的jar文件并尝试再次运行。