Google Web Toolkit(GWT)Web应用程序,MySQL在Eclipse中工作正常,但在openSUSE服务器上不工作
我已经编写了一个GWT web应用程序,它托管在外部openSUSE服务器上。我使用Google的GWT RPC与服务器通信,并将数据从客户端发送到服务器,在服务器上更新MySQL数据库。在Eclipse中,一切都运行得很好,但只要我将/war目录移动到服务器,包括java连接器所需的mysql-connector-java-5.1.18-bin.jar文件,服务器上的本地SQL数据库就不会得到更新。为什么它不起作用?这是某种mysql配置问题吗 我在服务器上设置了一个数据库,其名称和表与我在本地机器上调试Eclipse时使用的数据库完全相同。我没有从GWT获得任何错误,因此服务器访问工作正常,并且我的SQL日志文件也没有显示任何错误 我尝试重新启动mysql,检查mysql的3306端口是否打开并正在侦听,禁用了防火墙,并尝试了主机的localhost和127.0.0.1 我想不出是什么问题,请有人帮帮我!我快疯了 这是我的服务器端代码,它在Eclipse中运行良好,但在服务器上不起作用Google Web Toolkit(GWT)Web应用程序,MySQL在Eclipse中工作正常,但在openSUSE服务器上不工作,mysql,gwt,jdbc,opensuse,Mysql,Gwt,Jdbc,Opensuse,我已经编写了一个GWT web应用程序,它托管在外部openSUSE服务器上。我使用Google的GWT RPC与服务器通信,并将数据从客户端发送到服务器,在服务器上更新MySQL数据库。在Eclipse中,一切都运行得很好,但只要我将/war目录移动到服务器,包括java连接器所需的mysql-connector-java-5.1.18-bin.jar文件,服务器上的本地SQL数据库就不会得到更新。为什么它不起作用?这是某种mysql配置问题吗 我在服务器上设置了一个数据库,其名称和表与我在本
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;
}
}
我找到了解决问题的方法: 为了消除NoClassDefFoundError,应用程序使用的外部.jar文件应该直接复制到/WEB-INF/lib/中 Apache需要额外的jdbc msql配置步骤。首先,确保为SQL数据库设置了正确的授予权限 接下来,遵循他的解决方案,该解决方案取自: 首先,在$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>
您的catch-SQLException-ex日志语句将被注释掉。为什么?另外,永远不要以这种方式使用jdbc!阅读:谢谢你的帮助。我对java/mysql非常陌生,这是我的第一个web应用程序。你能解释一下使用jdbc的最佳方式是什么吗?另外,我现在不确定是否要编写catch语句。特别是如何在我的java路径中包含Version.class使用的tools.js,以避免出现NoClassDefFoundError。为什么需要版本类?阅读Logger.getLogger的手册和教程。如果您不知道如何处理异常,最好的方法是至少抛出新的RuntimeExceptionex。