有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java未找到合适的驱动程序:jdbc:google:mysql

我试图弄明白为什么我的应用引擎(端点)程序不会连接和/或加载jdbc google mysql驱动程序

我已在appengine web中启用了<use-google-connector-j>标记。xml并将其设置为指定的here

appengine-web。xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>...</application>
<version>1</version>
<threadsafe>true</threadsafe>
    <use-google-connector-j>true</use-google-connector-j>


    <system-properties>
    <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
</system-properties>
</appengine-web-app>

以及我尝试连接到Google Cloud SQL的端点方法:

 @ApiMethod(name = "lesson")
    public Lesson getLesson(@Named("id") int id) throws SQLException {


        // Create new lesson object
        Lesson l = new Lesson();

        // Create resultSet for database retrieval
        ResultSet rs = null;
        Connection c = null;
        // connect to the database
        // Connection c = DatabaseConnect();
        String talk = "";
        try {
            Class.forName("com.google.cloud.sql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.getStackTrace().toString();
            l.setLessonObjectives(e.getMessage());
        }

        String url = "jdbc:google:mysql://" + Constants.PROJECT_NAME + ":" + Constants.SQL_INSTANCE_NAME + "/" + Constants.SQL_DATABASE_NAME + "?user=root";
        try {
            c = DriverManager.getConnection(url);
            talk = talk + "..... And now i'm going to connect";
        } catch (SQLException e) {
            e.printStackTrace();
            l.setLessonDescription(e.getMessage() + " ---- " + e.getErrorCode() + " ----- " + e.getSQLState());
        }

        // check to make sure there is a connection available
        // execute query and save it in a result set.
        if(c != null) {
            try {

                rs = c.createStatement().executeQuery(
                        "SELECT * FROM se_lesson WHERE id = " + id);

                if (rs.first()) {

                    l.setLessonId(id);
                    l.setLessonName(rs.getString("lesson_name"));
                    l.setLessonObjectives(rs.getString("lesson_objectives"));
                    l.setLessonDescription(rs.getString("lesson_description"));
                    l.setLessonMinCoins(rs.getInt("lesson_min_coins"));
                    l.setLessonLevel(rs.getInt("lesson_level_id"));
                    l.setLessonColour(rs.getString("lesson_color"));

                } else {
                    l.setLessonId(-1);
                }

            } catch (SQLException e) {
                e.printStackTrace();
                l.setLessonId(-2);

            }
         }

        l.setLessonObjectives(talk);



        logger.info("Calling getLesson method");

        return l;
    }

通过API资源管理器调用端点时返回的错误消息是:

{
 "lessonId": 0,
 "lessonDescription": "No suitable driver found for jdbc:google:mysql://stark-english:content-instance/stark?user=root ---- 0 ----- 08001",
 "lessonObjectives": "I think its loaded",
 "lessonMinCoins": 0,
 "lessonLevel": 0,
 "kind": "stark#resourcesItem",
 "etag": "\"2PYCr435swl6FqpdQwvud90MSME/LgtErz4rWHsMTKvNQvVMw3CDWhw\""
}

知道为什么会发生这种情况吗?即使我正在调用google连接器并且它没有返回^{

更新1:

是的,我已经在我的Android studio项目中包含了mysql连接器。如下图所示:

Build library

App Engine Module libraries/dependencies


共 (1) 个答案

  1. # 1 楼答案

    CloudSQL驱动程序类名是:“com.mysql.jdbc.GoogleDriver”。您使用的是:“com.google.cloud.sql.jdbc.Driver”

    请注意,根据打开连接的位置(本地或服务器上),需要在驱动程序之间切换,如documentation中所述

    要在mysql服务器上本地打开连接,需要使用驱动程序:“com.mysql.jdbc.driver”,并适当修改连接URL