 |
January 2006 Archives
Accessing Derby from Creator
Posted by bleonard on January 30, 2006 at 05:13 PM | Permalink
| Comments (7)
Yes, I'm a NetBeans guy, but since Creator 2's been released, I can't stop playing with it. Since I've also been playing with Derby a bit lately, and Derby isn't one of the preconfigured database server types that ships with Creator, I thought a short blog entry might be in order.
Getting Started
- Download the latest official Derby release (10.1.2.1 at the time of this writing) and extract the archive.
- Set the DERBY_INSTALL environment variable to point to your Derby installation location. For me this is D:\db-derby-10.1.2.1-bin.
Start the Derby Server
- Open a command prompt and switch to the $DERBY_INSTALL/frameworks/NetworkServer/bin directory.
- Execute the setNetworkServerCP script, which sets the necessary Derby jars on the classpath.
- Execute the startNetworkServer script to start Derby. You should then see something like the following:
Add Derby Database Type to Creator
- Start Creator and switch to the Server's window (Ctrl+Alt+S).
- Right-click the Data Sources node and choose Edit Database Server Types.
- Click New and browse to the $DERBY_INSTALL/lib directory and select derbyclient.jar.
- Set the Display Name to Derby.
- Click Suggest to retrieve the Driver Class Name (org.apache.derby.jdbc.ClientDriver).
- Enter the following for the URL Template: jdbc:derby://#HOSTNAME:1527/#DATABASE. The completed dialog should look as follows.

Add a Derby Data Source
- Right-click the Data Sources node and choose Add Data Source. Set the following:
- Data Source Name: derbyDB.
- Server Type: Derby.
- Database Name: derbyDB
- User ID: APP
- Password: APP
- Database URL: jdbc:derby://localhost:1527/derbyDB;create=true
- Validation Table: SYS.SYSCHECKS
The completed dialog should look as follows:
- Click Test Connection:

- Then click OK and Add to add the derbyDB data source.
- By default, we see all the database schemas, including the sytem schemas. Right-click derbyDB and choose Modify Data Source.
- Switch to the Schemas tab and click Get Schemas.
- Click Clear and then select just the APP schema.
- Click Modify to close the Modify Data Source dialog.
Execute a SQL Script
- Customers.sql will create 3 tables and populate them with data - save it somewhere on your hard drive. Right-click the derbyDB data source and select View Data. Click the folder icon to Import a File containing SQL commands and open the Customers.sql file.
- Click Run Query. You'll see output like the following:
- Now right-click the derbyDB data source and select Refresh.
- Expand the Tables node to see the 3 tables that were created.

- Right-click the CUSTOMER_TBL and choose View Data:
Use the Data Source in an Application
- Create a new JSF Web Application project named DerbyData.
- Drag a Table component onto Page1.
- Switch to the Servers window (Ctrl+Alt+S) and drag the CUSTOMER_TBL from the derbyDB data source onto the table component on Page1.
- Run the project (Ctrl+F5).
Accessing Derby using JNDI
Posted by bleonard on January 17, 2006 at 04:31 PM | Permalink
| 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".
Getting Started With EJB 3.0
Posted by bleonard on January 05, 2006 at 12:23 PM | Permalink
| Comments (3)
If you read my bio, you'll see that I have a pretty deep background in J2EE technology. However, over the past year I've been focusing on other technologies, like the eBay SDK for Java and J2ME. In the mean time, J2EE, now Java EE, has sped on past. With the forthcoming release of Java EE 5, things are changing dramatically, specifically with regards to EJBs. Last month JSR 220 - Enterprise JavaBeans 3.0 released its Proposed Final Draft, so I thought this would be a good time to reacquaint myself with the technology. Now, how hard could it be? The sole purpose of EJB 3.0 is to "improve the EJB architecture by reducing its complexity from the developer's point of view". Still, EJB 3.0 introduces the developer to lots of new concepts, like annotations, dependency injection, persistence units and entity managers to name a few.
So I started with a simple stateless session bean example from JBoss' web site. Of course, I wanted to check out the new support for EJB 3.0 coming in the next version of NetBeans, so I thought I share with you my experience.
Getting Started
- Download JBoss AS 4.0.3 and install it with the EJB 3.0 profile selected.
- Download a development build of the NetBeans IDE. Choose Java EE 5 as the release version and Daily as the release type.
- Start the NetBeans IDE and switch to the Runtime tab (Ctrl+5). Right-click the Server's node and add the JBoss Application Server 4.0.3 that you just installed.
- You now see JBoss Application Server 4.0.3 in the Server's node. Right-click the node and select Start. You'll see the output from the JBoss server log in NetBeans's Output window and the JBoss server node will have green arrow indicating that's it is running:

Fast Track
If you just want to take the project and run, extract StatelessBeans.zip. Open the project in NetBeans. Deploy the project and run Client.java.
Create the Project
- Create a new Enterprise EJB Module project named StatelessBeans. Make sure the Server is JBoss and the J2EE version is Java EE 5.0.
- Add a new Session Bean (Ctrl+N - look under the Enterprise category) named CalculatorBean. Set the Package to org.jboss.tutorial.stateless.bean. Select the check boxes to create both Remote and Local interfaces.
- Expand the Enterprise Beans node of your project to see your CalculatorBean. Double-click it to open the EJB in the editor. Note that it's a simple POJO that implements two interfaces, CalculatorRemote and CalculatorLocal. Also note the @Stateless annotation. That's all that's required for the container to recognize this as a stateless session bean
- Now since both the local and remote interfaces will define the same method, create a Java Interface (Ctrl+N) named Calculator that they can both extend and add the following method signatures:
public int add(int x, int y);
public int subtract(int x, int y);
- Then update the local and remote interfaces to extend Calculator.
- Now switch back to the CalculatorBean and place your cursor on the class declaration (with the wavy red line). Right-click the light bulb that appears to implement the local and remote interface methods.
- Add the following to the body of the appropriate method:
return x + y;
return x - y;
- And our EJB is now complete. Before we can deploy it to JBoss however, we need to change the default archive extension name from "jar" to "ejb3". Switch to the Files tab (Ctrl + 2) and open the project.properties file in the nbproject directory. Change the jar.name property from StatlessBeans.jar to StatelessBeans.ejb3 and save the file (Ctrl+S).
- We can also delete the generated configuration files as they're empty - the container now knows what to do based on the Annotations. Switch back to the Projects tab (Ctrl+1) and expend the Configuration Files node. Delete ejb-jar.xml and jboss.xml.
- Right-click the StatelessBeans project and choose Deploy Project. If all goes well, you'll see the following in the output window:
Create a Test Client
We'll create a simple client to test the EJB.
- Create a new Java Main Class (Ctrl + N) named Client. Put it in the package org.jboss.tutorial.stateless.client.
- Add the following code to the main method:
InitialContext ctx = new InitialContext();
ctx.addToEnvironment("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
ctx.addToEnvironment("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
ctx.addToEnvironment("java.naming.provider.url","localhost");
Calculator calculator = (Calculator) ctx.lookup(CalculatorRemote.class.getName());
System.out.println("1 + 1 = " + calculator.add(1, 1));
System.out.println("1 - 1 = " + calculator.subtract(1, 1));
- Press Alt+Shift+F to fix the imports.
- The compiler's still unhappy. Place your cursor on the InitialContext line and right-click the light bulb that appears to add the throws clause for the NamingException.

- Now our JBoss plug-in is missing a couple of libraries required to run this class, so we need to add them. Right-click the Libraries node of the StatelessBeans project and choose Add Library. Click the Manage Libraries button and add a New Library named jboss-aop.deployer. Add jboss-aop-jdk50.jar and jboss-aspect-library-jdk50.jar from the jboss-4.0.3\server\default\deploy\jboss-aop.deployer directory. Click OK to return to the Add Library dialog and then Add Library to add the new library to your project. You now have this library available for any future needs.
- Run the Client (Shift+F6) and you'll see the following output:

Note, to get rid of the log4j warnings, save this log4j.xml to your src/java directory and build the project.
|