I have created a Dynamic Web Page project in Eclipse and coded a Servlet. I have added derby.jar as a library. When I'm going to deploy the project, I export it as a .war-file and then start Jetty with java -jar start.jar
. The servlet works fine without database code. But when I try to use the JavaDB database, the JDBC driver couldn't be found. How do I use a JavaDB database from my Servlet in Jetty?
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.