Wednesday, October 6, 2010

JAVA and MySQL

For some time now, I've been wanting to create a simple java application that will connect to a mysql database, do a simple select (from a table) and insert into (a table). As a programming challenge, that is really at the simple end of the scale. After looking at some sample code out there on the web, typing in a few lines of java, and that it, right?

Well, I'm in a UNIX environment which assumes I know a lot more than I actually do. So, after typing in the sample code and making a few small adjustments like the database name, user, password, url, stuff like that, issued the javac compile and it compiled just fine. When I went to run it, the program threw an exception that it could not find a necessary path. Painstakingly, I went through each line of java source code and compared to the sample, plus making sure those other things were correct. They were, it compile's fine and at run time it still throws an exception. Not one to give up, just put it aside for a while.

So, couple hours later, picked it up again. I made a good effort to consider other possibilities. Remembering a coworkers conversation about the CLASSPATH environment variable, I took a look at some other java code that is in production, specifically, to answer the question, just how does this thing start up. And what do you know, I was not defining the CLASSPATH variable. The most fundamental of all things java. A quick setting of that guy inside of the terminal window, then re-ran the code and it worked. In order not to forget such a critical thing, I went ahead and places those "setenv" lines into the source code as comments, (would not be executed at run time - or ever) to serve as reminders. I built upon that success and also coded in an INSERT INTO statement too.

I have that code saved off in my $HOME/dev/src directory to serve as an example for the future. I may end up giving that sample code to my fellow engineers in the case they would need it for their coding assignments. That's cool, I don't mind prototyping stuff either.

No comments:

Post a Comment