Posted by gsporar
on October 11, 2005 at 2:47 PM PDT
My blog the other day about using the HTML/JSP palette left out a step: creating the data source and the connection pool.
The other day I wrote a blog entry about the new HTML/JSP palette in the NetBeans Integrated Development Environment (IDE) v5.0 beta release .
I got a comment on that entry pointing out that SQL code should not be used in a JSP. I would tend to agree that a Model-View-Controller approach would be the right way to go for most applications. But for small or prototype applications, taking advantage of the JSTL's SQL tags might be okay. This is not a new debate - for more thoughts see Sue Spielman's Practical JSTL, Part 2 .
If SQL tags are used in a JSP, however, then at the very least you should try to avoid using the <sql:setDataSource> tag, which is what I had in my example the other day. There's a much better way to set up the data source and the NetBeans IDE makes it easy to do. The steps I describe here work with Sun's Java System Application Server Platform Edition (v8.1), which can be downloaded with the IDE.
Since I was using the sample Pointbase database that comes with the IDE, I can start it by choosing Tools > Pointbase Database > Start Local Pointbase Database. To get connected, I use the Runtime window where I can expand the Databases node and then right-click the entry for the sample database and choose Connect. From here it's a three step process: create the data source and connection pool, register them, and then put references in the deployment descriptors so that they can be used.
I right-click my project and then choose New > File/Folder. Under Sun Resources there is an entry for JDBC Resource, as shown below.
After I click the Next button, the New JDBC Resource wizard starts up and on its first screen I specify the name and that I want a new connection pool created as well.
I don't need to make any changes to the default Properties, so I click Next to advance to the Database Connection screen. I choose to have the IDE extract information from my existing connection.
On the connection pool properties screen all I have to do is put in the password for the connection (and press the Enter key!). Then I clicked Finish.
The end result was I had two new entries in my project's Server Resources: one for the data source and one for the connection pool, as shown below.
On both of those Server Resources, I had to right-click and choose Register from the context menu. Note: Make sure you start the application server before doing this - initially I didn't have it running and therefore got an error message. Once the resources were registered, I was able to see them under the application server's JDBC entry over in the Runtime window:
One of the nicest features of the NetBeans IDE when it comes to J2EE development is that it has editors specifically for dealing with deployment descriptors. So all I had to do was double-click my project's web.xml file to start the visual editor. I clicked the References tab and then clicked the Add button. In the dialog that popped up I put in the name of the Resource, as shown below.
After I clicked OK, the IDE modified not only web.xml, but also the server specific deployment descriptor, sun-web.xml. In the top half of the screen shot below I've switched the web.xml editor to XML mode so that you can see the actual changes made (lines 13 through 18). In the bottom half of the screen I've opened sun-web.xml and selected the new reference.
The end result is that now I have a data source I can reference from my JSP. So the code in the JSP no longer needs the <sql:setDataSource> tag. Which means it now looks like this:
Note the new value for the dataSource attribute on line 16.
Naturally, the resource created here could be used in other places besides the JSP. If I'm in a servlet I can right-click and choose Enterprise Resources > Use Database. The IDE will generate the necessary code to do the lookup for me. But that's a topic for a different blog entry....
My thanks to Petr Blaha for pointing me in the right direction on this topic and a big thanks to Geertjan Wielenga for providing detailed instructions.