从Google App Engine Java连接到本地MySQL实例时出错。

时间:2016-04-04 01:12:44

标签: java mysql google-app-engine

我使用gradle构建插件创建了一个google app engine java项目。应用引擎版本为1.9.34。我正在尝试将本地开发服务器连接到本地mysql数据库以进行开发和测试。我正在尝试连接本地数据库,如下所示,

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

但是不断收到以下错误,

  

SEVERE:调用后端方法时发生异常       java.lang.ExceptionInInitializerError       at java.lang.Class.forName0(Native Method)       在java.lang.Class.forName(Class.java:191)       在com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)       在com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:65)       在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)       在java.sql.DriverManager.getConnection(DriverManager.java:571)       在java.sql.DriverManager.getConnection(DriverManager.java:215)       在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)       在com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:130)       在com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:363)       在com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:113)       在com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:71)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:637)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:717)       在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166)       在com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:128)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:50)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)       在com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)       在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)       在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)       在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)       在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)       在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)       在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)       在com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)       在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)       在com.google.appengine.tools.development.JettyContainerService $ ApiProxyHandler.handle(JettyContainerService.java:513)       在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)       在org.mortbay.jetty.Server.handle(Server.java:326)       在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)       在org.mortbay.jetty.HttpConnection $ RequestHandler.content(HttpConnection.java:938)       在org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)       在org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)       在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)       在org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)       在org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)   引起:java.security.AccessControlException:访问被拒绝(" java.lang.RuntimePermission"" modifyThreadGroup")       at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)       at java.security.AccessController.checkPermission(AccessController.java:559)       在java.lang.SecurityManager.checkPermission(SecurityManager.java:549)       在com.google.appengine.tools.development.DevAppServerFactory $ CustomSecurityManager.checkPermission(DevAppServerFactory.java:429)       在com.google.appengine.tools.development.DevAppServerFactory $ CustomSecurityManager.checkAccess(DevAppServerFactory.java:454)       在java.lang.ThreadGroup.checkAccess(ThreadGroup.java:315)       在java.lang.Thread.init(Thread.java:391)       在java.lang.Thread.init(Thread.java:349)       在java.lang.Thread。(Thread.java:445)       在java.util.TimerThread。(Timer.java:499)       在java.util.Timer。(Timer.java:101)       在java.util.Timer。(Timer.java:146)       在com.mysql.jdbc.ConnectionImpl。(ConnectionImpl.java:208)       ... 53更多

自从过去几天以来一直困在这里。任何帮助表示赞赏。

谢谢!

3 个答案:

答案 0 :(得分:0)

您是否在app引擎gradle中添加了以下依赖项?

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

如果是,您还可以检查将应用引擎的版本更改为1.9.18,它与我合作,我在过去的版本1.9.22以及之后的版本中遇到了问题。

答案 1 :(得分:0)

首先,确保您已在appengine-web.xml中启用了MySQL 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>

然后使用类似以下代码段的内容连接到数据库。

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
}

您可以找到更多详情here

答案 2 :(得分:0)

其实我已尝试过上面提到的两个选项。这两个更改都是使应用程序正常工作所必需的,但我的案例中的问题是mysql连接器版本。我正在使用5.1.16,但后来我将其更新为5.1.38并修复了问题。

谢谢大家的帮助和建议! :)