Showing posts with label package. Show all posts
Showing posts with label package. Show all posts

JDBC Data Access

I have been talking about this new project I am working on. There are a lot of URLs that I want to scrape. The first task is to put the list into a database. I have decided to use JDBC to access the database from my Java program.

Java Standard Edition 6 comes with the "java.sql" package. You should import all the classes in that package. JDBC requires a driver to run. You load the driver in your code with a Class.forName.

Any SQL can be sent to the database using JDBC. By default, the objects used to connect to the database automatically commit after each statement. You should use the close method on the connection object to free database resources.

Setting the CLASSPATH

I continue to read ahead in my Java class textbook. Reading alone does not make the concepts sink in. So I try to do each and every exercise in the book. Today I had some free time at work. I decided to try one of the exercises from the section I was reading. The exercise instructed me to use a class that had a source listing in the book.

My instructor provided me with the source code for the listings in the book. This helped so I did not need to type in the code. I compiled the class provided to me by the book. Then I coded up a simple second class in another file which made use of the class from the book. However the Java compiler complained that it did not know the other class name.

This was very strange. I had already compiled the other class. Everything was in the same directory. I was at a loss. I tried to import the other class. That did not work. The other class was not in a package. I could have sworn that this worked before. If the other class is in the same directory you can use it.

Then it dawned on my. I bet there was something wrong with the CLASSPATH. I checked out the system variables and found that the current directory (.) was not a part of the CLASSPATH. I quickly added it to the beginning of the CLASSPATH. Now this makes sense. Java won’t consider code in the current directory unless that directory is part of the CLASSPATH.