Sunday Apr 24, 2005

NetBeans IDE 4.1: No More Worrying About Database Connections

In one of my first blog entries (Servlet Connection Pooling for Dummies), I explained the cool NetBeans IDE feature that allows you to see a JSP's translated servlet after it has been deployed. It has to be deployed first before you can see it, because the servlet that you see is translated by the server, hence it needs to be there before it can be translated. In that blog entry I mentioned that one way to connect to a database from a servlet is to simply copy the translated servlet and use that. However, as also pointed out in that blog entry, the disadvantage of that method is that each server translates a JSP page differently, so that you'd end up with an application that isn't really portable. On top of that, it seems like the server generates a lot of garbage and, as a result, I've found the translated servlet quite hard to read.

I've been generally quite daunted by servlets -- and Java source files in general -- and have tended to stick to JSP pages. So, I was pleasantly surprised this morning, while updating the Connecting to Databases chapter for the Using NetBeans IDE 4.1 Guide, to discover that the IDE can generate a whole bunch of code for you -- specifically, the code that makes the connection with the database -- which is not only helpful in general but also means that there's at least one part of my code that I can rely on as being correct. When debugging my Java source code in the past, it was often hard to know exactly where a problem originated. Now, when I'm creating Java source files that access a database, I can be sure that at least the database connection is not the place where things have gone haywire.

And it's all made possible with this dialog box:

It appears when I right-click anywhere in the body of a class in the Source Editor and choose Enterprise Resources > Use Database. After specifying the JNDI name of the data source, together with the connection URL, I click OK and get this:

   private javax.sql.DataSource getPoolDB() throws javax.naming.NamingException {
        javax.naming.Context c = new javax.naming.InitialContext();
        return (javax.sql.DataSource) c.lookup("java:comp/env/jdbc/poolDB");
    }

Before, when I had to come up with the code for the database connection myself, I'd always be thinking, "Now is this the correct format for my connection string?" But now the IDE does all that thinking for me. And the IDE also gives me this in my in my web.xml file:

  <resource-ref>
    <description>jdbc:pointbase://localhost:9092/sample [pbpublic on PBPUBLIC]</description>
    <res-ref-name>jdbc/poolDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

...and this in my sun-web.xml file (but nothing in context.xml in 4.1 -- it's a known issue):

  <resource-ref>
    <res-ref-name>jdbc/poolDB</res-ref-name>
    <jndi-name>jdbc/poolDB</jndi-name>
  </resource-ref>

So now, instead of thinking about the connection, I'm left thinking about how to use it. Pretty cool.

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2005 »
SunMonTueWedThuFriSat
     
1
2
8
9
10
14
16
18
22
23
29
       
Today