0

我编写了一个托管在外部 openSUSE 服务器上的 GWT Web 应用程序。我使用 Google 的 GWT RPC 与服务器通信,并将数据从我的客户端发送到 MySQL 数据库更新的服务器。在 Eclipse 中一切正常,但只要我将 /war 目录移动到服务器(包括 java-connector 所需的 mysql-connector-java-5.1.18-bin.jar 文件),我的本地 SQL 数据库服务器没有更新。为什么它不起作用?这是某种mysql配置问题吗?

我已经在服务器上设置了一个数据库,其名称和表与我的本地机器上的数据库完全相同(我在那里使用 Eclipse 进行调试)。我没有从 GWT 收到任何错误(因此服务器访问工作正常),我的 SQL 日志文件也没有显示错误。

我尝试重新启动 mysql,检查 mysql 的端口 3306 是否已打开并正在侦听,禁用防火墙,并为主机尝试了 localhost 和 127.0.0.1。

我无法弄清楚问题是什么,请有人帮助我!我在这里疯了!

这是我的服务器端代码,它在 Eclipse 中运行良好,但在服务器上却不行!

package com.mycompany.mywebapp.server;

@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService {

public String[] greetServer(String[] input) {

    //Data is entered into MySQL database on server side 
    Connection con = null; 
    Statement st = null; 

    String url = "jdbc:mysql://127.0.0.1:3306/testdb";
    String user = "username";
    String password = "pass";

    try{
        con = DriverManager.getConnection(url,user,password); //establishes connection to database
        st = con.createStatement(); //object for sending SQL statements to database 

        for (int i=0;i<input.length;i++){
            String DataToSend = input[i];
            String part1 = DataToSend.substring(0,DataToSend.indexOf("["));
            String part2 = DataToSend.substring(DataToSend.indexOf("["),DataToSend.indexOf("]")+1);
            //store values into mysql database
            String query = "INSERT INTO boundingboxes(name, box) VALUES('"+part1+"','"+part2+"')";
            st.executeUpdate(query);
        }

    }catch (SQLException ex){
        Logger lgr = Logger.getLogger(Version.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);

    }finally{
        //make sure to avoid null pointerexception 
        try{
            if (st !=null){
                st.close();
            }
            if (con != null){
                con.close();
            }
        }catch (SQLException ex){
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }

    return input; 
}



}
4

1 回答 1

0

我找到了解决问题的方法:要摆脱 NoClassDefFoundError,应将应用程序使用的外部 .jar 文件直接复制到 /WEB-INF/lib/ 中。

Apache 需要额外的 jdbc-msql 配置步骤。首先,确保您为 SQL 数据库设置了正确的 GRANT 权限。

接下来,按照他的解决方案取自:http ://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html#connector-j-usagenotes-tomcat

首先,将 Connector/J 附带的 .jar 文件安装在 $CATALINA_HOME/common/lib 中,以便容器中安装的所有应用程序都可以使用它。

接下来,通过在定义 Web 应用程序的上下文中将声明资源添加到 $CATALINA_HOME/conf/server.xml 来配置 JNDI 数据源:

<Context ....>

...

<Resource name="jdbc/MySQLDB"
       auth="Container"
       type="javax.sql.DataSource"/>

<!-- The name you used above, must match _exactly_ here!

The connection pool will be bound into JNDI with the name
"java:/comp/env/jdbc/MySQLDB"
-->

<ResourceParams name="jdbc/MySQLDB">
<parameter>
 <name>factory</name>
 <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Don't set this any higher than max_connections on your
 MySQL server, usually this should be a 10 or a few 10's
 of connections, not hundreds or thousands -->

    <parameter>
      <name>maxActive</name>
      <value>10</value>
    </parameter>

 <!-- You don't want to many idle connections hanging around
   if you can avoid it, only enough to soak up a spike in
   the load -->

   <parameter>
    <name>maxIdle</name>
    <value>5</value>
   </parameter>

 <!-- Don't use autoReconnect=true, it's going away eventually
 and it's a crutch for older connection pools that couldn't
 test connections. You need to decide whether your application
 is supposed to deal with SQLExceptions (hint, it should), and
 how much of a performance penalty you're willing to pay
 to ensure 'freshness' of the connection -->

  <parameter>
   <name>validationQuery</name>
   <value>SELECT 1</value> <-- See discussion below for update to this option -->
  </parameter>

  <!-- The most conservative approach is to test connections
  before they're given to your application. For most applications
  this is okay, the query used above is very small and takes
  no real server resources to process, other than the time used
  to traverse the network.

  If you have a high-load application you'll need to rely on
  something else. -->

  <parameter>
   <name>testOnBorrow</name>
   <value>true</value>
  </parameter>

   <!-- Otherwise, or in addition to testOnBorrow, you can test
   while connections are sitting idle -->

   <parameter>
    <name>testWhileIdle</name>
    <value>true</value>
   </parameter>

  <!-- You have to set this value, otherwise even though
   you've asked connections to be tested while idle,
   the idle evicter thread will never run -->

 <parameter>
  <name>timeBetweenEvictionRunsMillis</name>
  <value>10000</value>
 </parameter>

 <!-- Don't allow connections to hang out idle too long,
 never longer than what wait_timeout is set to on the
 server...A few minutes or even fraction of a minute
 is sometimes okay here, it depends on your application
 and how much spikey load it will see -->

 <parameter>
  <name>minEvictableIdleTimeMillis</name>
  <value>60000</value>
 </parameter>

 <!-- Username and password used when connecting to MySQL -->

 <parameter>
  <name>username</name>
  <value>someuser</value>
 </parameter>

 <parameter>
  <name>password</name>
  <value>somepass</value>
 </parameter>

 <!-- Class name for the Connector/J driver -->

 <parameter>
  <name>driverClassName</name>
  <value>com.mysql.jdbc.Driver</value>
 </parameter>

 <!-- The JDBC connection url for connecting to MySQL, notice
 that if you want to pass any other MySQL-specific parameters
 you should pass them here in the URL, setting them using the
 parameter tags above will have no effect, you will also
 need to use &amp; to separate parameter values as the
 ampersand is a reserved character in XML -->

 <parameter>
  <name>url</name>
  <value>jdbc:mysql://localhost:3306/test</value>
 </parameter>
于 2011-12-03T18:39:03.573 回答