GWT中的MYSQL驱动程序错误

时间:2013-03-01 15:15:41

标签: java eclipse gwt jdbc

我一直在尝试将我的GWT项目连接到我的MySQL数据库几个小时但是在加载驱动程序时我一遍又一遍地发现一个奇怪的错误。

服务器的代码是: 请注意,我只是尝试加载mysql的驱动程序,并使用GWT附带的示例项目。 saveToDB(String)是有趣的部分。

package test.server;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import test.client.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
        GreetingService {

    public String greetServer(String input){
        // Verify that the input is valid. 
        saveToDB(input);
        String serverInfo = getServletContext().getServerInfo();
        String userAgent = getThreadLocalRequest().getHeader("User-Agent");

        // Escape data from the client to avoid cross-site script vulnerabilities.
        input = escapeHtml(input);
        userAgent = escapeHtml(userAgent);


        return "Hello, " + input + "!<br><br>I am running " + serverInfo
                + ".<br><br>It looks like you are using:<br>" + userAgent;
    }

    /**
     * Escape an html string. Escaping data received from the client helps to
     * prevent cross-site script vulnerabilities.
     * 
     * @param html the html string to escape
     * @return the escaped string
     */
    private String escapeHtml(String html) {
        if (html == null) {
            return null;
        }
        return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
                .replaceAll(">", "&gt;");
    }

    public void saveToDB(String input){
        String query = "INSERT INTO `oryx.model`" +
                "VALUES ('" + input + "');";
                // Save the model that is in variable "message" (very long string/text)
                //try {

                Connection conn = getConn();
               // Statement update = (Statement) conn.createStatement();
                //update.executeUpdate(query);

                //get last id 
               /*ResultSet result = (ResultSet) update.getGeneratedKeys(); 
                if (result != null && result.next()) { 
                    int rsId = result.getInt(1);  
                }

                result.close();
                update.close();
                conn.close();
            } catch(SQLException e) {
                System.err.println("Mysql Statement Error: " + query);
                e.printStackTrace();
            }*/

    }
    private Connection getConn() {

        Connection conn     = null;
        String url          = "jdbc:mysql://localhost:3306/";
        String db           = "mysqld";
        String driver       = "com.mysql.jdbc.Driver";
        String user         = "user";
        String pass         = "pass";



        try {
            System.out.println("ERROR 1");
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("ERROR 2");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            System.out.println("ERROR 3");
        } 
        //conn = DriverManager.getConnection(url+db, user, pass);

        //SQL EXCEPTION
        //System.err.println("Mysql Connection Error: ");
        //CLASS NOT FOUND
      //e.printStackTrace();
            return conn;
}
}

我总是得到错误:

  

SEVERE:javax.servlet.ServletContext log:调度时出现异常   传入的RPC调用com.google.gwt.user.server.rpc.UnexpectedException:   服务方法&#39; public abstract java.lang.String   test.client.GreetingService.greetServer(java.lang.String中)&#39;扔了一个   意外异常:java.lang.ExceptionInInitializerError at   com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)     在   com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)     在   com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)     在   com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)     在   com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:637)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:717)at at   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:123)     在   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:61)     在   org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)     在   com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)     在   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.BackendServersFilter.doFilter(BackendServersFilter.java:97)     在   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:94)     在   org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)     在   com.google.appengine.tools.development.JettyContainerService $ ApiProxyHandler.handle(JettyContainerService.java:409)     在   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)at   org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)at at   org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)at at   org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)     在   org.mortbay.thread.QueuedThreadPool $ PoolThread.run(QueuedThreadPool.java:582)   引起:java.lang.ExceptionInInitializerError at   java.lang.Class.forName0(Native Method)at   java.lang.Class.forName(未知来源)at   test.server.GreetingServiceImpl.getConn(GreetingServiceImpl.java:88)     在   test.server.GreetingServiceImpl.saveToDB(GreetingServiceImpl.java:56)     在   test.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:22)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)     在   com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)     ... 38更多引起:java.security.AccessControlException:access   否认(java.lang.RuntimePermission modifyThreadGroup)at   java.security.AccessControlContext.checkPermission(未知来源)at   java.security.AccessController.checkPermission(未知来源)at   java.lang.SecurityManager.checkPermission(未知来源)at   com.google.appengine.tools.development.DevAppServerFactory $ CustomSecurityManager.checkPermission(DevAppServerFactory.java:289)     在   com.google.appengine.tools.development.DevAppServerFactory $ CustomSecurityManager.checkAccess(DevAppServerFactory.java:314)     在java.lang.ThreadGroup.checkAccess(未知来源)at   java.lang.Thread.init(未知来源)at   java.lang.Thread。(未知来源)at   com.mysql.jdbc.NonRegisteringDriver $ 1(NonRegisteringDriver.java:90)     在   com.mysql.jdbc.NonRegisteringDriver。(NonRegisteringDriver.java:89)     ......还有49个

如果您需要,可以查看以下网址的整个代码: http://www19.zippyshare.com/v/9248211/file.html

非常感谢所有帮助。

//萨米

1 个答案:

答案 0 :(得分:1)

  

引起:java.security.AccessControlException:访问被拒绝(java.lang.RuntimePermission modifyThreadGroup)

  

在com.google.appengine.tools.development.DevAppServerFactory $ CustomSecurityManager

是有趣的部分。

您不能在AppEngine上使用MySQL:https://developers.google.com/appengine/docs/java/jrewhitelist

除非您使用CloudSQL,否则它具有特定的API:https://developers.google.com/appengine/docs/java/cloud-sql/developers-guide