简体   繁体   English

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

[英]How to use a JavaDB database from a Java Servlet in Jetty?

I have created a Dynamic Web Page project in Eclipse and coded a Servlet. 我在Eclipse中创建了一个动态Web页面项目并编写了一个Servlet。 I have added derby.jar as a library. 我已将derby.jar添加为库。 When I'm going to deploy the project, I export it as a .war -file and then start Jetty with java -jar start.jar . 当我要部署项目时,我将其导出为.war -file,然后使用java -jar start.jar启动Jetty。 The servlet works fine without database code. 没有数据库代码,servlet工作正常。 But when I try to use the JavaDB database, the JDBC driver couldn't be found. 但是当我尝试使用JavaDB数据库时,找不到JDBC驱动程序。 How do I use a JavaDB database from my Servlet in Jetty? 如何在Jetty中使用Servlet中的JavaDB数据库?

I try to use an in-memory-database for testing, and my code looks like this: 我尝试使用内存数据库进行测试,我的代码如下所示:

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

Here is the Exception that is thrown: 这是抛出的异常:

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)

You did not load the driver class before obtaining a connection: 在获取连接之前,您没有加载驱动程序类:

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

Just as a side note, you might also want to refactor the process of loading the driver class and using connections to a single point in your application. 作为旁注,您可能还想重构加载驱动程序类并使用连接到应用程序中的单个点的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM