0

I've installed postgresql 9.1 for Windows but I can't connect to it using JDBC.

I've downloaded the JDBC jar file and placed it in C:\Program Files\Java\jre7\lib\postgresql-9.1-901.jdbc4.jar, my CLASSPATH is: .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Java\jre7\lib\postgresql-9.1-901.jdbc4.jar

This is my Java code to create the connection:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;
import java.io.*;

public class CreateInsert extends Object {
    public static void main (String args[]) {

        //Create the connection
        String driverName = "org.postgresql.Driver";
        String connectURL = "jdbc:postgresql://localhost/postgres";
        String USERNAME = "postgres";
        String PASSWORD = "password";
        Connection con = null;
        try {
            Class.forName("org.postgresql.Driver");
            con = DriverManager.getConnection(connectURL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            System.out.println("Error creating class: "+e.getMessage());
            System.out.println("The Driver was not found, Please check driver location, classpath, username/password and server url settings");
            System.exit(0);
        } catch (SQLException e) {
            System.out.println("Error creating connection: "+e.getMessage());
            System.exit(0);
        }
    }
}

And I get the error "Error creating class: org.postgresql.Driver"

Any ideas as to what's wrong?

Thanks.

2
  • 1
    The classpath actually used depends on how exactly you're executing the application. So, tell a bit more about that. Commented Dec 17, 2011 at 12:21
  • 1
    I'm using JCreator to compile and run. Commented Dec 17, 2011 at 12:46

2 Answers 2

2

I'm using JCreator to compile and run.

The CLASSPATH environment variable is only used when you use java.exe command without -cp, -classpath and -jar arguments. Any other way you use to execute the Java application ignores this environment variable. This includes executing the application inside an IDE like Eclipse, Netbeans and JCreator.

In an IDE, you instead need to drop the JAR in the project and add it to the "Build Path" if not done automatically by the IDE yet, depending on the project's structure. This is often a matter of rightclicking the JAR in project and choosing "Add to Build Path" somewhere in the context menu.

Forget about using the CLASSPATH environment variable. It was a mistake by Sun. They thought to convince starters by avoiding to enter the -cp or -classpath arguments everytime for javac/java commands. But it end up to be only more confusing to starters as they interpret that environment variable as "the" classpath.

Sign up to request clarification or add additional context in comments.

Comments

-1

The problem is Classpath or the Driver you used. Try to run this using - java -cp C:\Program Files\Java\jre7\lib\postgresql-9.1-901.jdbc4.jar CreateInsert

And send the report

2 Comments

C:\Users\F\Desktop\Coursework>java -cp C:\Program Files\Java\jre7\lib\postgre sql-9.1-901.jdbc4.jar CreateInsert Error: Could not find or load main class Files\Java\jre7\lib\postgresql-9.1-901. jdbc4.jar I tried java CreateInsert and it seems to be working. Will edit in a few mins.
Using java CreateInsert seems to have worked. Thanks for everyones help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.