0

当我尝试通过 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");
     }
}
4

1 回答 1

0

缺少“套接字工厂”库可能是个问题。您使用的构建工具是什么?是 Maven 还是 Gradle?

尝试在你的项目中包含这个库,看看它是否有效。

马文:

<dependency>
    <groupId>com.google.cloud.sql</groupId>
    <artifactId>postgres-socket-factory</artifactId>
    <version>1.0.15</version>
</dependency>

摇篮:

compile 'com.google.cloud.sql:postgres-socket-factory:1.0.15'

还有我的朋友,请不要将所有内容都塞进连接字符串中。在DriverManager 构造函数中使用属性

String url = "jdbc:postgresql:///<DATABASE_NAME>";

Properties props = new Properties();
props.setProperty("user", "<user>");
props.setProperty("password", "<password>");
props.setProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory");
props.setProperty("cloudSqlInstance", "<instance>");

// Fill rest of the properties here (if any)
Connection conn = DriverManager.getConnection(url, props);


编辑:比在代码中硬编码这些道具更好的方法是将所有配置数据存储在 XML 文件或配置文件中,并从<yourProject>.xml或加载属性<yourProject>.config

然后使用properties.load(...)or properties.loadFromXML(...)文档

于 2020-04-19T07:35:31.383 回答