ClassNotFoundException: com.mysql.jdbc.GoogleDrive

2019-01-26 22:10发布

问题:

I wonder how come this error is thrown while hosting my project in APP ENGINE, I have added lots of logging just for analysis sake. When I use the com.mysql.jdbc.Driver using ip from my local it works. Kindly help !!

    String name = "Vinodh";
    String url = null;
    try {
        Class.forName("com.mysql.jdbc.GoogleDriver");
        url = "jdbc:google:mysql://xxxxxxx:xxxxxx/vinodh?user=root&password=xxxxxx";

        // Statements allow to issue SQL queries to the database
        log.info("Initiate Connection");
        Connection conn = DriverManager.getConnection(url);
        log.info("Got Connection");
        Statement statement = conn.createStatement();
        // Result set get the result of the SQL query
        ResultSet resultSet = statement

                .executeQuery("select * from Family");
        log.info("Entering While");
        while(resultSet.next()){
             log.info("Entered While");
            String test = resultSet.getString("Name");
            System.out.println(test);

            name = test+test+test;
        }

回答1:

As shwown in this tutorial, during development you should use the normal mysql driver and only appengine use the Google mysql driver

  if (SystemProperty.environment.value() ==
      SystemProperty.Environment.Value.Production) {
    // Load the class that provides the new "jdbc:google:mysql://" prefix.
    Class.forName("com.mysql.jdbc.GoogleDriver");
    url = "jdbc:google:mysql://your-project-id:your-instance-name/guestbook?user=root";
  } else {
    // Local MySQL instance to use during development.
    Class.forName("com.mysql.jdbc.Driver");
    url = "jdbc:mysql://127.0.0.1:3306/guestbook?user=root";
  }

Also double check that you have enabled MySQL Connector/J for your application (it's not done by default)

https://developers.google.com/appengine/docs/java/cloud-sql/#enable_connector_j

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