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

时间:2015-05-20 02:30:03

标签: java mysql google-app-engine jdbc google-cloud-endpoints

我试图找出为什么我的App Engine(端点)程序无法连接和/或加载jdbc google mysql驱动程序。

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

应用服务引擎-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 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工作室项目中包含了mysql连接器。如下图所示:

Build library

App Engine Module libraries/dependencies

1 个答案:

答案 0 :(得分:3)

CloudSQL驱动程序类名称为:“com.mysql.jdbc.GoogleDriver”。您正在使用:“com.google.cloud.sql.jdbc.Driver”。

请注意,根据您打开连接的位置(本地或服务器),您需要按照documentation中的说明在驱动程序之间切换。

要在mysql服务器上本地打开连接,您需要使用驱动程序:“com.mysql.jdbc.Driver”并相应地修改连接URL。