1
votes

I have created a google app engine java project using the gradle build plugin. The app engine version is 1.9.34. I am trying to connect local dev server to local mysql db for development and testing purposes. I am trying to connect local db as follows,

 try {
        Class.forName("com.mysql.jdbc.Driver");
        url = "jdbc:mysql://localhost/testDB?user=root";

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    Connection conn = null;
    try {
        conn = (Connection) DriverManager.getConnection(url, "root", "admin123$");

        String statement = "INSERT INTO entries (firstName, lastName, phoneNo, email) VALUES( ? , ? , ?, ?)";
        java.sql.PreparedStatement stmt = conn.prepareStatement(statement);
        stmt.setString(1, newContact.getFirstName());
        stmt.setString(2, newContact.getLastName());
        stmt.setString(3, newContact.getPhoneNumber());
        stmt.setString(4, newContact.getEmail());
        int success = 2;
        success = stmt.executeUpdate();
        if (success == 1) {

            System.out.println("Insert Successful");
        } else if (success == 0) {
            System.out.println("Insert error");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

But keep getting the following error,

SEVERE: exception occurred while calling backend method java.lang.ExceptionInInitializerError at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:191) at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70) at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:65) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282) at java.sql.DriverManager.getConnection(DriverManager.java:571) at java.sql.DriverManager.getConnection(DriverManager.java:215) at com.aarnam.whatsthescore.apis.AddressBookApi.createNewContact(AddressBookApi.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:130) at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:363) at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:113) at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:71) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:128) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:50) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366) at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349) at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:513) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThreadGroup") at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372) at java.security.AccessController.checkPermission(AccessController.java:559) at java.lang.SecurityManager.checkPermission(SecurityManager.java:549) at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkPermission(DevAppServerFactory.java:429) at com.google.appengine.tools.development.DevAppServerFactory$CustomSecurityManager.checkAccess(DevAppServerFactory.java:454) at java.lang.ThreadGroup.checkAccess(ThreadGroup.java:315) at java.lang.Thread.init(Thread.java:391) at java.lang.Thread.init(Thread.java:349) at java.lang.Thread.(Thread.java:445) at java.util.TimerThread.(Timer.java:499) at java.util.Timer.(Timer.java:101) at java.util.Timer.(Timer.java:146) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:208) ... 53 more

Have been stuck at this since last few days. Any help is appreciated.

Thanks!!

3

3 Answers

0
votes

First, make sure you have enabled MySQL Connector/J in appengine-web.xml as follows.

<?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>

Then use something like the below code snippet to connect to the database.

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/your-database?user=root";
} else {
    // Local MySQL instance to use during development.
    Class.forName("com.mysql.jdbc.Driver");
    url = "jdbc:mysql://127.0.0.1:3306/your-database?user=root";

    // Alternatively, connect to a Google Cloud SQL instance using:
    // jdbc:mysql://ip-address-of-google-cloud-sql-instance:3306/your-database?user=root
}

You can find more details here.

0
votes

Have you added below dependency in your app engine gradle ?

compile 'mysql:mysql-connector-java:5.1.16'

If yes, you can also check to change the version of app engine to 1.9.18, it works with me, I had issues in past with version 1.9.22 and later ones.

0
votes

Actually I have tried both of the options suggested above. Both the changes are necessary to get the app working but the issue in my case was the mysql connector version. I was using 5.1.16 but then I updated it to 5.1.38 and that fixed the problem.

Thank you all for your help and suggestions! :)