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

4
我正在努力找出为什么我的App Engine(Endpoints)程序无法连接和/或加载jdbc谷歌mysql驱动程序。
我已经在appengine-web.xml中启用了标签,并将其设置为TRUE,如此处所指定。

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;
    }

当我通过APIs Explorer调用端点时返回的错误消息是:
{
 "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连接器,它也没有返回ClassNotFoundException?
更新1:
是的,我已经在我的Android Studio项目中包含了mysql连接器。如下所示:

Build library

App Engine Module libraries/dependencies


我想你应该查看https://dev59.com/nmox5IYBdhLWcg3wzng0。基本上将mysql-driver jar添加到路径并部署它。 - Allahbaksh Asadullah
谢谢,@AllahbakshAsadullah,但这并没有帮助我。我目前正在使用Android Studio部署我的App Engine项目。我已经检查过确保mysql连接器已经包含在内了。 - markbratanov
1
你能否确定哪个类找不到。mysql-connector是否已经在部署文件夹中,该文件夹正在上传到appengine。 - Allahbaksh Asadullah
1个回答

3
云SQL的驱动程序类名为:“com.mysql.jdbc.GoogleDriver”。您正在使用的是:“com.google.cloud.sql.jdbc.Driver”。
请注意,根据您打开连接的位置(本地或服务器),您需要根据文档中的说明在驱动程序之间切换。
要在mysql服务器上本地打开连接,您需要使用驱动程序:“com.mysql.jdbc.Driver”,并适当修改连接URL。

这就是问题所在了。我不确定为什么会变成com.google.cloud.sql.jdbc.Driver,我敢说我是直接从文档复制和粘贴代码的,但可能并不是这样。 - markbratanov
谢谢,顺便说一句。让我省了很大的麻烦。 - markbratanov
3
没问题。我曾经犯过的一些最严重的错误都是由于复制/粘贴造成的;-) - Mo'in Creemers
我遇到了相同的错误,不是复制粘贴,而是自动导入。我不知道为什么AppEngine Sdk有那个库。 - Gonzalo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接