我试图让 JDO 在 Servlet 中工作;只是一个简单的基本 Servlet。我知道 Servlet 可以连接到我的数据库,因为非 JDO servlet 可以很好地完成它,并从数据库中打印出数据(以下是简单 Servlet 的开头):
StringBuilder sb = new StringBuilder();
Connection conn = null;
try {
String userName = "jdo";
String password = "jdo";
String url = "jdbc:mysql://192.168.2.203:3306/jdo";
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
conn = DriverManager.getConnection( url, userName, password );
sb.append( "Database connection established" );
Statement s = conn.createStatement();
s.executeQuery( "select id, make, model from car limit 1" );
ResultSet rs = s.getResultSet();
while ( rs.next() ) {
int id = rs.getInt( "id" );
String make = rs.getString( "make" );
String model = rs.getString( "model" );
sb.append( "id = " + id + ", make = " + make + ", model = " + model + "\n" );
}
rs.close();
s.close();
}
没关系,StringBuilder 从附加的数据库中获取数据没有问题。但是,以下是行不通的。它会在最后一行(第 28 行)抛出一个堆栈跟踪,我将在此处粘贴(此代码也是一个简单 Servlet 的开头,只是与包含先前代码的 Servlet 不同):
Properties p = new Properties();
p.setProperty( "javax.jdo.option.ConnectionDriverName", "com.mysql.jdbc.Driver" );
p.setProperty( "javax.jdo.option.ConnectionURL", "jdbc:mysql://192.168.2.203:3306/jdo" );
p.setProperty( "javax.jdo.option.ConnectionUserName", "jdo" );
p.setProperty( "javax.jdo.option.ConnectionPassword", "jdo" );
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory( p ); // this is line 28
堆栈跟踪在这篇文章的底部,因为它相当长。
这两个 Servlet 存在于同一个 war 文件中,其中是 MySQL Connector/J jar 文件:
mkns@squeeze:~/workspace/JdoServlet$ jar tf dist/jdoservlet.war | grep mysql
WEB-INF/lib/mysql-connector-java-5.1.6.jar
我很困惑为什么 DataNucleus 库找不到 MySQL 连接器/J 驱动程序 jar 文件?我可能在这里遗漏了明显的东西,但是我已经尽可能多地剥离了我的 Servlet,以查看问题出在哪里(如上所述),但我不知道我哪里出错了。谁能指出我犯的错误?
我正在使用最新版本的 DataNucleus,3.0.9。
--- 8< ---
现在是堆栈跟踪;这是从 Tomcat6 catalina 日志文件中获取的完整堆栈跟踪。请注意,我的应用程序中的行位于 com.xxxxxxxxxx.jdoservlet.* 的屏蔽包名称中,因此如果您查找它,您可以确定我的应用程序引发异常的位置。
12-Apr-2012 18:48:47 org.datanucleus.store.rdbms.RDBMSStoreManager <init>
SEVERE: Failed initialising database.
No suitable driver found for jdbc:mysql://192.168.2.203:3306/jdo
org.datanucleus.exceptions.NucleusDataStoreException: No suitable driver found for jdbc:mysql://192.168.2.203:3306/jdo
at org.datanucleus.store.rdbms.ConnectionFactoryImpl$ManagedConnectionImpl.getConnection(ConnectionFactoryImpl.java:459)
at org.datanucleus.store.rdbms.RDBMSStoreManager.<init>(RDBMSStoreManager.java:264)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.datanucleus.plugin.NonManagedPluginRegistry.createExecutableExtension(NonManagedPluginRegistry.java:681)
at org.datanucleus.plugin.PluginManager.createExecutableExtension(PluginManager.java:290)
at org.datanucleus.NucleusContext.createStoreManagerForProperties(NucleusContext.java:468)
at org.datanucleus.NucleusContext.initialise(NucleusContext.java:280)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.freezeConfiguration(JDOPersistenceManagerFactory.java:591)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.createPersistenceManagerFactory(JDOPersistenceManagerFactory.java:326)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.getPersistenceManagerFactory(JDOPersistenceManagerFactory.java:195)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.jdo.JDOHelper$16.run(JDOHelper.java:1956)
at java.security.AccessController.doPrivileged(Native Method)
at javax.jdo.JDOHelper.invoke(JDOHelper.java:1951)
at javax.jdo.JDOHelper.invokeGetPersistenceManagerFactoryOnImplementation(JDOHelper.java:1159)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:839)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:698)
at com.xxxxxxxxxx.jdoservlet.Test.process(Test.java:28)
at com.xxxxxxxxxx.jdoservlet.AbstractServlet.doPost(AbstractServlet.java:35)
at com.xxxxxxxxxx.jdoservlet.AbstractServlet.doGet(AbstractServlet.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.2.203:3306/jdo
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.apache.commons.dbcp.DriverManagerConnectionFactory.createConnection(DriverManagerConnectionFactory.java:78)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1148)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
at org.datanucleus.store.rdbms.ConnectionFactoryImpl$ManagedConnectionImpl.getConnection(ConnectionFactoryImpl.java:444)
... 39 more
Nested Throwables StackTrace:
java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.2.203:3306/jdo
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.apache.commons.dbcp.DriverManagerConnectionFactory.createConnection(DriverManagerConnectionFactory.java:78)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1148)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
at org.datanucleus.store.rdbms.ConnectionFactoryImpl$ManagedConnectionImpl.getConnection(ConnectionFactoryImpl.java:444)
at org.datanucleus.store.rdbms.RDBMSStoreManager.<init>(RDBMSStoreManager.java:264)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.datanucleus.plugin.NonManagedPluginRegistry.createExecutableExtension(NonManagedPluginRegistry.java:681)
at org.datanucleus.plugin.PluginManager.createExecutableExtension(PluginManager.java:290)
at org.datanucleus.NucleusContext.createStoreManagerForProperties(NucleusContext.java:468)
at org.datanucleus.NucleusContext.initialise(NucleusContext.java:280)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.freezeConfiguration(JDOPersistenceManagerFactory.java:591)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.createPersistenceManagerFactory(JDOPersistenceManagerFactory.java:326)
at org.datanucleus.api.jdo.JDOPersistenceManagerFactory.getPersistenceManagerFactory(JDOPersistenceManagerFactory.java:195)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.jdo.JDOHelper$16.run(JDOHelper.java:1956)
at java.security.AccessController.doPrivileged(Native Method)
at javax.jdo.JDOHelper.invoke(JDOHelper.java:1951)
at javax.jdo.JDOHelper.invokeGetPersistenceManagerFactoryOnImplementation(JDOHelper.java:1159)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:839)
at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:698)
at com.xxxxxxxxxx.jdoservlet.Test.process(Test.java:28)
at com.xxxxxxxxxx.jdoservlet.AbstractServlet.doPost(AbstractServlet.java:35)
at com.xxxxxxxxxx.jdoservlet.AbstractServlet.doGet(AbstractServlet.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)