Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
Right, so I found the PostgreSQL JDBC driver here. Downloaded it and put it in my home folder which I then added to the CLASSPATH by adding the following lines to my .profile file:

Code:
CLASSPATH=".:/Users/simon/postgresql.jar"

export CLASSPATH
when I echo $CLASSPATH in Terminal it shows up correctly, yet when I try and load the JDBC PostgreSQL driver in Netbeans it says class not found. I even added it as a library as well to the Netbeans project file.

I've also tried it from the command line and text editor and it still gives the following exception:

typhoon:Desktop simon$ javac Main.java
Main.java:14: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("org.postgresql.Driver");
^
1 error
I am most likely doing something silly as this is my first foray into Java.

Source code:
Code:
package javaone;

import java.sql.*;

public class Main
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Class.forName("org.postgresql.Driver");
    }

}

Edit: Interestingly enough, the Netbeans 6.5 IDE allows me to connect to the database and use SQL commands using the JDBC driver but still will not run the code. That must mean that the driver is somewhere and is working it just is not picking it up at the project level for some strange reason.
 
Last edited:

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
The error points to the Class.forName not being found, which is in the base class of Java, so an import is not the issue.

Try wrapping it in a try /catch block and see if that fixes it - that's what the error msg is saying.

edit - yep, that'll fix it.
Code:
public class Main
{
    /**
     * @param args the command line arguments
     */
	public static void main(String[] args)
	{
		try { 
			Class.forName("org.postgresql.Driver");
		}
		catch (Exception e) {} 
	}
}
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
Actually, it's not throwing an exception; you aren't catching one! Many method calls, like the call to forName that you are performing, require that you place try-catch blocks around the code for various required exceptions. In this case, the Class.forName( Ljava/lang/String ) call requires that you catch the possible java.lang.ClassNotFoundException that might be thrown. Try adding:
Code:
try {
/* reflection call here */
} catch( java.lang.ClassNotFoundException cnfe ) {
    System.err.println( "Error: Caught ClassNotFoundException: " + cnfe );
    cnfe.printStackTrace();
}

The Class.forName( Ljava/lang/String ) can also throw java.lang.LinkageError and java.lang.ExceptionInInitializerError but these are considered runtime exceptions and you aren't required to catch them.

I would suggest reading up on Java exception handling as it is VERY important and, as you've seen, if it's not performed properly (or at all), most things won't compile. If you check the JavaDoc for whatever class you are trying to use it will list all of the required exceptions and most of the runtime ones that a class can throw.
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
Ah, thanks guys. I didn't realise that Java forced you to catch exceptions if they are generated. I thought error checking was optional like C.

Doesn't help that the Java book I am learning from is ancient and deals with Java 1.1.
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
Ah, thanks guys. I didn't realise that Java forced you to catch exceptions if they are generated. I thought error checking was optional like C.

Doesn't help that the Java book I am learning from is ancient and deals with Java 1.1.

Java doesn't force you to catch ALL of the possible exceptions, only the ones that the method definition specifies that you must catch. For example:
Code:
public void someMethod( boolean abool ) throws Exception {
    if( abool ) {
        throw new Exception( "Some critical error" );
    } else {
        throw new InvalidOperationException( "You don't have to catch me!" );
    }
}

In the above code, the method specifies that the caller MUST catch Exception if it is thrown but the method also throws InvalidOperationException. Since InvalidOperationException is a subclass of RuntimeException, we can simply ignore it and the runtime will catch it, eventually.

If you want something that's fairly up to date, check out Sun's online Java tutorials. They are pretty helpful, have lots of sample code and, given how out of date your book it, it's probably better than what you're using.
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
If you want something that's fairly up to date, check out Sun's online Java tutorials. They are pretty helpful, have lots of sample code and, given how out of date your book it, it's probably better than what you're using.

Thanks for the link. Those tutorials are actually much better than I thought they would be. I've managed to get myself much more up to speed with Java now in a relatively short space of time.

Glad I've ditched PHP now, that was an exercise in pain.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.