当我尝试运行通过 jdbc 连接到谷歌云实例的脚本时,我不断收到此错误。完全不确定如何解决它:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/cloud/sql/core/CoreSocketFactory
at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:73)
at org.postgresql.core.PGStream.<init>(PGStream.java:73)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:93)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
at org.postgresql.Driver.makeConnection(Driver.java:459)
at org.postgresql.Driver.connect(Driver.java:261)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at App.main(App.java:12)
Caused by: java.lang.ClassNotFoundException: com.google.cloud.sql.core.CoreSocketFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 11 more
连接到 db 的脚本(“..”引用了出于隐私原因未包括在内的实例名称和密码):
import java.sql.Connection;
import java.sql.DriverManager;
public class App
{
public static void main(String args[])
{
Connection c = null;
try
{
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql:///<postgres>?cloudSqlInstance=<...:database>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<postgres>&password=<...>");
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}