Google Web Toolkit (GWT) Web 应用程序与 MySQL 在 Eclipse 中工作正常,但在 openSUSE 服务器上无法工作

发布于 2024-12-19 15:53:58 字数 2232 浏览 4 评论 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; 
}



}

I've written a GWT web app which is hosted on an external openSUSE server. I use Google's GWT RPC to communicate with the server and send data from my client to the server where a MySQL database gets updated. Everything works perfectly fine in Eclipse but as soon as I move my /war directory to the server (including the necessary mysql-connector-java-5.1.18-bin.jar file for the java-connector), the local SQL database on my server is not getting updated. Why is it not working? Is this some kind of mysql config issue?

I have set up a database on the server with the exact same name and table as the one on my local machine (where I was debugging with Eclipse). I do not get any errors from GWT (so the server access works fine) and my SQL log files also show no errors.

I have tried to restart mysql, checked that port 3306 for mysql is open and listening, disabled the firewall, and tried both localhost and 127.0.0.1 for the host.

I cannot figure out what the problem is, PLEASE someone help me! I'm going crazy here!

Here is my server side code which works fine in Eclipse but not on the server!

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



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2024-12-26 15:53:58

我找到了解决我的问题的方法:
要摆脱 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

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

接下来,通过将声明资源添加到定义 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 & 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>

I found the solution to my problems:
To get rid of the NoClassDefFoundError, the external .jar files used by your app should be copied into the /WEB-INF/lib/ directly.

Apache requires an extra configuration step for jdbc-msql. Firstly, make sure that you have set the correct GRANT permissions set up for your SQL database.

Next, follow his solution which is taken from: http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html#connector-j-usagenotes-tomcat

First, install the .jar file that comes with Connector/J in $CATALINA_HOME/common/lib so that it is available to all applications installed in the container.

Next, Configure the JNDI DataSource by adding a declaration resource to $CATALINA_HOME/conf/server.xml in the context that defines your web application:

<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 & 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文