首页 > 解决方案 > 在android studio中使用JDBC,找不到合适的驱动程序

问题描述

我是这个 JDBC 驱动程序的新手。我正在寻找将用户数据从我的 android 应用程序保存到谷歌云 mySQL 的方法。我碰巧遇到 JDBC 可能会完成这项工作。

但是,我遇到了这个错误No suitable driver found for jdbc:mysql://google/waveUserData?cloudSqlInstance=wavdata&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false

我已经下载了JDBC驱动并放入了/library/java/extensions

请帮我解决这个问题,或者请向我推荐一种有效地将用户数据从应用程序传输到谷歌云 mysql 的方法。

这是我指的代码:https ://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/blob/master/examples/compute-engine/src/main/java/com/google/cloud /sql/mysql/example/ListTables.java

package com.google.cloud.sql.mysql.example;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* A sample app that connects to a Cloud SQL instance and lists all       available tables in a database.
*/
public class ListTables {
  public static void main(String[] args) throws IOException,   SQLException {
    // TODO: fill this in
   // The instance connection name can be obtained from the instance overview page in Cloud Console
// or by running "gcloud sql instances describe <instance> | grep connectionName".
String instanceConnectionName = "<insert_connection_name>";

// TODO: fill this in
// The database from which to list tables.
String databaseName = "mysql";

String username = "root";

// TODO: fill this in
// This is the password that was set via the Cloud Console or empty if never set
// (not recommended).
String password = "<insert_password>";

if (instanceConnectionName.equals("<insert_connection_name>")) {
  System.err.println("Please update the sample to specify the instance connection name.");
  System.exit(1);
}

if (password.equals("<insert_password>")) {
  System.err.println("Please update the sample to specify the mysql password.");
  System.exit(1);
}

//[START doc-example]
String jdbcUrl = String.format(
    "jdbc:mysql://google/%s?cloudSqlInstance=%s"
        + "&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
    databaseName,
    instanceConnectionName);

Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
   //[END doc-example]

try (Statement statement = connection.createStatement()) {
  ResultSet resultSet = statement.executeQuery("SHOW TABLES");
  while (resultSet.next()) {
    System.out.println(resultSet.getString(1));
      }
    }
  }
}

标签: androidmysqlandroid-studiojdbcgcloud

解决方案


推荐阅读