1

我在 Eclipse 中创建了一个动态网页项目并编写了一个 Servlet。我已将derby.jar添加为库。当我要部署项目时,我将它导出为.war文件,然后使用 .war 启动 Jetty java -jar start.jar。servlet 无需数据库代码即可正常工作。但是当我尝试使用 JavaDB 数据库时,找不到 JDBC 驱动程序。如何在 Jetty 中使用我的 Servlet 中的 JavaDB 数据库?

我尝试使用内存数据库进行测试,我的代码如下所示:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                         throws ServletException, IOException {

    String sql =    "DECLARE GLOBAL TEMPORARY TABLE SESSION.mytable "+
                    "(id int, message varchar(10)) NOT LOGGED";

    String connURL = "jdbc:derby:memory:memdatabase;create=true";


    try {
        Connection conn = DriverManager.getConnection(connURL);
        PreparedStatement ps = conn.prepareStatement(sql);
        boolean success = ps.execute();
        System.out.println("created: " + success);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

这是抛出的异常:

java.sql.SQLException: No suitable driver found for jdbc:derby:memory:memdatabas
e;create=true
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at com.example.MyJavaServlet.doPost(MyJavaServlet.java:59)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:598
)
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java
:486)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.j
ava:119)
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.jav
a:499)
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandl
er.java:233)
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandl
er.java:1065)
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:
413)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandle
r.java:192)
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandle
r.java:999)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.j
ava:117)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(Cont
extHandlerCollection.java:250)
        at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerColl
ection.java:149)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper
.java:111)
        at org.eclipse.jetty.server.Server.handle(Server.java:350)
        at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(Abstrac
tHttpConnection.java:454)
        at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpC
onnection.java:900)
        at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.conten
t(AbstractHttpConnection.java:954)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:851)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)

        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnecti
on.java:77)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEn
dPoint.java:606)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEnd
Point.java:46)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPoo
l.java:603)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool
.java:538)
        at java.lang.Thread.run(Unknown Source)
4

1 回答 1

3

在获得连接之前您没有加载驱动程序类:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = ...

顺便说一句,您可能还想重构加载驱动程序类和使用连接到应用程序中单个点的过程。

于 2012-04-27T06:41:57.653 回答