 |
Accessing Derby using JNDI
Posted by bleonard on January 17, 2006 at 04:31 PM | Comments (3)
After reading my Derby Tutorial, a co-worker asked me about using JNDI to access the Derby database. Here are the steps required to access Derby as a resource configured in Tomcat 5.5.
Getting Started
- First, follow the Derby Tutorial at least through the step Create Table Wizard. This will get Derby properly configured with NetBeans and give us at least one table to work with, Friends.
- Copy the derbyclient.jar to Tomcat's lib directory. You'll find derbyclient.jar in /db-derby-10.1.2.1-bin/lib. Copy it to /netbeans-4.1/enterprise1/jakarta-tomcat-5.5.7/common/lib. On the Macintosh, you'll copy the jar to
/Applications/NetBeans.app/Contents/Resources/NetBeans/enterprise2/jakarta-tomcat-5.5.9/common/lib.
Fast Track
If you just want to take the project and run, extract DerbyJNDI.zip. Open the project in NetBeans and press F6 to run it.
Create the Project
- Create a new Web Application project named DerbyJNDI. Make sure the Server is Bundled Tomcat (5.5.9).
- Expand the Configuration Files node. Double-click context.xml to open it in the editor. Add the following to configure the resource:
<Context path="DerbyJNDI">
<Resource name="jdbc/Sample" auth="Container"
type="javax.sql.DataSource" username="nbuser" password="nbuser"
driverClassName="org.apache.derby.jdbc.ClientDriver" url="jdbc:derby://localhost/Sample"
maxActive="8" maxIdle="4"/>
</Context>
- Expand the Web Pages > WEB-INF node. Double-click web.xml to open it in the graphical XML editor. Click the References button at the top. Expand the Resource References section and choose Add. Set the following values in the dialog:

- Add a new Servlet named DerbyJNDIServlet in package derby.
- Remove the comments surrounding the out.println statements.
- Between the <body> tags, add the following:
out.println("<body>");
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/Sample");
Connection conn = ds.getConnection();
Statement s = conn.createStatement();
s.execute("Select * From \"NBUSER\".\"Friends\"");
ResultSet rs = s.getResultSet();
while (rs.next()) {
out.println(rs.getString("NAME") + " is my friend.");
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
out.println("</body>");
- Press Alt+Shift+F to fix the imports. Select the appropriate packages:

- Set the application's relative URL to the servlet. Open the project's properties, select the Run category and set the Relative URL to /DerbyJNDIServlet.
- Make sure the Derby Database is started (Tools > Derby Database > Start Derby Database).
- Press F6 to test run the application.
Note, if you are prompted for the Tomcat Manager username and password, you'll find it in the jakarta-tomcat-5.5.9_base/conf/tomcat-users.xml file in your user directory. You can locate your User Dir by choosing Help > About and then selecting the Detail tab.
If all goes well, you should see the output in your browser "Scott McNealy is my friend".
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Excellent tutorial!
One problem though, the stack trace doesn't display in the HTML output. If you make any mistakes it's more difficult to see what the error is.
I recommend that you change the call to printStackTrace() to include the PrintWriter reference as its argument as follows:
ex.printStackTrace( out );John
Posted by: jpmasseria on May 16, 2006 at 07:00 PM
-
The paths have changed since this article was written.
This is the current path on OS X with the cp command as you would use in you shell:
[you]$ cp /Users/[you]/.netbeans/5.5/db-derby-10.1.3.1-bin/lib/derbyclient.jar /Applications/NetBeans.app/Contents/Resources/NetBeans/enterprise3/apache-tomcat-5.5.17/common/lib/derbyclient.jar
But I don't really understand why you have to do this manually when both Tomcat and Derby are bundled with Netbeans. Why do we have to do this plumbing?
Posted by: christian_koefoed on August 23, 2007 at 05:57 PM
-
Hi Christian. Yes, Tomcat and Derby are bundled with NetBeans, but that doesn't mean they know about one another. This blog shows you how to configure Derby as a resource so you can cleanly look it up in your code by simply calling:
DataSource ds = (DataSource) envCtx.lookup("jdbc/Sample");
Otherwise, everytime you wanted to use the database you would need to specify the driver, username, password, JDBC URL, etc. Hope that helps, Brian
Posted by: bleonard on September 27, 2007 at 12:17 PM
|