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:

    <description>jdbc:pointbase://localhost:9092/sample [pbpublic on PBPUBLIC]</description>

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


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


Is there a similar mechanism that works in the Netbeans platform? I.e., if I create a desktop app that needs to connect to a database, is there a similar mechanism? Thanks, great work, BTW.

Posted by Frank Sommers on May 27, 2005 at 01:45 PM PDT #

I haven't tried it, Frank, but there should be no difference. At least, if I understand you correctly: you want to create a module that accesses a database, right? You'd create it in the IDE, using the functionality described above, and then build your module. Once you have your module, you could install it in the NetBeans platform and it should work from there without any problem. Maybe I'll try this soon and blog about it. Thanks for the question.

Posted by Geertjan on May 29, 2005 at 03:41 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« August 2016