markbratanov
markbratanov

Reputation: 898

No suitable driver found: jdbc:google:mysql

I'm trying to figure out why my App Engine (Endpoints) program will not connect and/or load the jdbc google mysql driver.

I have enabled the <use-google-connector-j> tag in appengine-web.xml and set it to TRUE as specified 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>

And my endpoint method that tries to connecto the 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;
    }

And the error message returned when I call the endpoint through the APIs Explorer is:

{
 "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\""
}

Any idea why this would happen even if I'm calling the google connector and it isn't returning a ClassNotFoundException?

Update 1:

Yes I've included the mysql connector in my Android studio project. As shown below:

Build library

App Engine Module libraries/dependencies

Upvotes: 4

Views: 5014

Answers (1)

Mo&#39;in Creemers
Mo&#39;in Creemers

Reputation: 1149

The CloudSQL driver class name is: "com.mysql.jdbc.GoogleDriver". You are using: "com.google.cloud.sql.jdbc.Driver".

Note that depending on where you are opening the connection (locally or on the server) you will need to switch between driver as explained in the documentation.

To open the connection locally on a mysql server you need to use driver: "com.mysql.jdbc.Driver" and also modify the Connection URL appropriately.

Upvotes: 3

Related Questions