首页 > 解决方案 > 在访问谷歌云 sql 时,我通过 java 遇到错误

问题描述

我想通过 Java 访问谷歌云 SQL,但我遇到了一些问题,请提出一些我必须做的事情

// Project ID of the project that contains the instance.
String project = "xxxxxx"; // TODO: Update placeholder value.

// Database instance ID. This does not include the project ID.
String instance = "instance"; // TODO: Update placeholder value.

GoogleAuthenticationService google = new GoogleAuthenticationService();
google.genaraeteGoogleCredentials();

SQLAdmin sqlAdminService = createSqlAdminService(google.getGoogleCregentials());
SQLAdmin.Instances.Get request = sqlAdminService.instances().get(project, instance);

DatabaseInstance response = request.execute();

// TODO: Change code below to process the `response` object:
System.out.println(response);
}

public static SQLAdmin createSqlAdminService(GoogleCredential googleCregentials) throws IOException, GeneralSecurityException {

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

GoogleCredential googleCregentials = GoogleCredential.getApplicationDefault();
if (credential.createScopedRequired()) {
  credential =
      credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
}

return new SQLAdmin.Builder(httpTransport, jsonFactory, googleCregentials)
    .setApplicationName("Google-SQLAdminSample/0.1")
    .build();
}
}

我想访问云 SQl,但出现错误

Exception in thread "main" java.lang.NoSuchFieldError: preferIPv6Address
at java.base/java.net.InetAddress.init(Native Method)
at java.base/java.net.InetAddress.<clinit>(InetAddress.java:341)
at java.base/java.net.PlainSocketImpl.initProto(Native Method)
at java.base/java.net.PlainSocketImpl.<clinit>(PlainSocketImpl.java:43)
at java.base/java.net.Socket.setImpl(Socket.java:505)
at java.base/java.net.Socket.<init>(Socket.java:86)
at java.base/javax.net.ssl.SSLSocket.<init>(SSLSocket.java:162)
at java.base/sun.security.ssl.BaseSSLSocketImpl.<init>(BaseSSLSocketImpl.java:61)
at java.base/sun.security.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:103)
at java.base/sun.security.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:72)
at java.base/sun.net.www.protocol.https.HttpsClient.createSocket(HttpsClient.java:413)
at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:162)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
at java.base/sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:265)
at java.base/sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:372)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1356)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1331)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:241)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:108)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:79)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:995)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:322)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:346)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:577)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:372)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:494)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.fromStreamUser(GoogleCredential.java:860)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.fromStream(GoogleCredential.java:257)
at com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider.getCredentialUsingWellKnownFile(DefaultCredentialProvider.java:249)
at com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider.getDefaultCredentialUnsynchronized(DefaultCredentialProvider.java:117)
at com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider.getDefaultCredential(DefaultCredentialProvider.java:91)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:213)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:191)
at com.prod.prodProject.TestGoogleCredentiels.createSqlAdminService(TestGoogleCredentiels.java:41)
at com.prod.prodProject.TestGoogleCredentiels.main(TestGoogleCredentiels.java:28)

标签: javagoogle-cloud-sql

解决方案


推荐阅读