如何在Jetty中使用Java Servlet中的JavaDB数据库?

时间:2012-04-27 06:39:01

标签: java servlets jdbc derby javadb

我在Eclipse中创建了一个动态网页项目并编写了一个Servlet。我已将 derby.jar 添加为库。当我要部署项目时,我将其导出为 .war -file,然后使用java -jar start.jar启动Jetty。没有数据库代码,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)

1 个答案:

答案 0 :(得分:3)

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

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

作为旁注,您可能还想重构加载驱动程序类并使用连接到应用程序中的单个点的过程。