Stupid newbie question re: Java, JDBC and PostgreSQL

Discussion in 'Mac Programming' started by Cromulent, Nov 29, 2008.

  1. Cromulent, Nov 29, 2008
    Last edited: Feb 9, 2011

    Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #1
    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:

    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.
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  3. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    Current directory should also be searched for classes.
     
  4. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    As yes - the colon is the path separator char on a Mac. Nevermind.
     
  5. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    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) {} 
    	}
    }
    
    
     
  6. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #6
    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.
     
  7. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    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.
     
  8. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #8
    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.
     
  9. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    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.
     

Share This Page