December 22, 2002

Java, day #3: JDBC, MySQL, and a Rant

Well, it's not really day #3. But I've spent a fair amount of time on Friday and over the weekend reading a copy of "The Java Programming Language" to refresh my memory on all this new-fangled Java stuff.

I decided it was time I wrote a stand-alone Java program to do... something. Normally, you'd expect someone to write the standard "Helllo, world." program and build from there. No, not me. That's too easy and likely to work on the first try.

No, for me a good first program is one that connects to MySQL, runs a query, and spits out the results. In this case, the query was to be SHOW DATABASES. As you might guess, that asks MySQL for a list of all the databases it knows about. Think of it as "Hello, MySQL."

So I headed over to the MySQL web site to download Connector/J the official MySQL JDBC driver.

Once it was unpacked and installed, I began working on the code. I had found some example code on-line was close to what I needed, so I adapted it a bit. Once it compiled successfully, I spent a lot of time chasing down a really dumb run-time exception. It seemed that Java could find the driver I had just installed. According to the docs, I could either put a whole directory tree into my CLASSPATH or just use the single JAR file. I opted for the jar file, because it seems cleaner. I'd rather deal with a single file. I'm sure that's a bit of a performance hit, but it's only startup cost and I really don't care about the startup cost. I'll be writing server-side code anyway, so it generally starts once and then runs for a long time.

Anyway, to make a long story short, Sun did something rather stupid. I wasted a good hour because I should have put an explicit reference to the jar file in my $CLASSPATH. It's not sufficient to simply drop the jar file into one of the directories specified in $CLASSPATH.

At this point, I felt like the guy in that one switch parody who, after upgrading to OS X from OS 9, says "What the fuck?!"

Do the morons at Sun (one of the oldest Unix companies!) not realize how Unix path variables normally work? Paths specify directory names, not zip, tar, jar, or any other type of archive files.

Note to Sun: Thanks for breaking 20+ years of tradition so that I could waste an hour scatching my ass on this problem. There's nothing like pulling dumb tricks like this to make the barrier to entry just a little bit higher.

Why can't Java just check all the *.jar files that happen to live in given directory? Is that too damned hard?

Anyway, the good news is that after I fixed that stupid problem (I copied the jar file to $JAVA_HOME/jre/lib/ext instead of endorsing Sun's bad decision), the code worked on the first try. Granted, the code's a bit verbose compared to the Perl equivelant, but it's a nice little class that could be reused in some other simple proof-of-concept code. It all reminds me a bit of learning C++ way back when.

Excellent. My version of "Hello, MySQL." works. That wasn't so hard after all.

Next up, learning a bit more about the JDBC API, connection pooling in Java, and related fun stuff. Oh, I also want to play with Connector/J's failover feature. It sounds like a neat idea that I just might have to implement in DBIx::DWIW for Perl.

Posted by jzawodn at 11:21 PM

I've mostly switched...

In thinking a bit more about what I wrote here, I've realized that I've mostly switched. At home I use OS X as my "desktop" and Linux on the server. At work it's Linux on the "desktop" and FreeBSD on the server (usually). Only two things have been bothering me about the TiBook, and I'm close to having both solved.

The first was memory. I currently have 384MB in the TiBook. It came with 256MB. I just orded a 512MB SODIMM so that I can bring it up to 768MB. Once I've done that, I suspect that it'll be a lot more usable when I've got lots of stuff open.

The second problem was finding an alternative terminal. I've mostly settled on GLTerm because it's really, really fast and contains All The Right Fonts. On the downside, I need to cought up some cash to register it and have to deal with it freaking out every once in a while. I'm also keeping my eye on iTerm. It's a bit young but shows some real promise. It is faster than Terminal.app but isn't even close to GLTerm when it comes to raw speed. Plus, using iTerm means using a sucky font. But that may change someday.

Posted by jzawodn at 09:11 PM