当我尝试通过 jdbc 连接到谷歌云实例时出现此错误。不太确定为什么会发生,但这是完整的错误:
Exception in thread "main" java.lang.NoSuchMethodError: 'com.google.api.client.json.JsonParser com.google.api.client.json.JsonFactory.createJsonParser(java.io.InputStream, java.nio.charset.Charset)'
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:77)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:73)
at com.google.auth.oauth2.GoogleCredentials.fromStream(GoogleCredentials.java:101)
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentialsUnsynchronized(DefaultCredentialsProvider.java:107)
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:55)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:66)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:48)
at com.google.cloud.sql.core.CoreSocketFactory$ApplicationDefaultCredentialFactory.create(CoreSocketFactory.java:299)
at com.google.cloud.sql.core.CoreSocketFactory.getInstance(CoreSocketFactory.java:126)
at com.google.cloud.sql.core.CoreSocketFactory.connect(CoreSocketFactory.java:185)
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/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at App.main(App.java:12)
这是脚本(出于隐私考虑,实例名称和密码已被“...”无效:
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");
}
}