Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help to connect to PostgreSql

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help to connect to PostgreSql

    Hi all,

    I'm a java newbie and need help to connect to a postgresql database from NetBeans.
    I've managed to register the postgresql driver in service page in NetBeans and establish
    a connection to my database, but how should I do it from my code.

    This is my code copied from examples on the net:

      private void btnOpenDbActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        Connection conn = null;
     
        try
        {
    //      Class.forName("???");
          conn = DriverManager.getConnection("jdbc:postgresql:5432//localhost/mapobjectdb",
                 "postgres", "secret");
        }
        catch (SQLException se)
        {
          System.out.println("Couldn't connect: print out a stack trace and exit.");
          se.printStackTrace();
          System.exit(1);
        }
     
      if (conn != null)
        System.out.println("Connected to the database!");
      else
        System.out.println("Couldn't connect");
      }
    What am I supposed to enter in Class.forName

    Regards
    stab


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help to connect to PostgreSql

    Try:
    Class.forName("org.postgresql.Driver");

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help to connect to PostgreSql

    Downloaded "JDBC4 Postgresql Driver, Version 9.0-801.zip", unzipped it, didn't get a jarfile, instead a filestructure like:
    postgresql-9.0-801.jdbc4
    META-INF
    org
    postgresql
    a lot subdirectories + the following files
    Driver$1.class
    Driver$ConnectThread.class
    Driver.class
    PGConnection.class
    PGNotification.class
    PGRefCursorResultSet.class
    PGResultSetMetaData.class
    PGStatement.class

    My CLASSPATH is like :
    .;C:\Projekt\Java\postgresql-9.0-801.jdbc4\org\postgresql

    Modified my code according to your suggestion to:
    try
    {
    Class.forName("org.postgresql.Driver");
    }
    catch (ClassNotFoundException se)
    {
    System.out.println("Doesn't find driver for PostgreSQL");
    se.printStackTrace();
    System.exit(1);
    }

    but I still get "Doesn't find driver for PostgreSQL"

    How am I supposed to specify the driver in Class.forName("???"); or is the error in my CLASSPATH?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help to connect to PostgreSql

    The links on this page point to the driver jar file which should be placed on your classpath. Don't unpack/unzip this file (which it looks like you've done based upon the directory structure you posted)

  5. The Following User Says Thank You to copeg For This Useful Post:

    stab (June 3rd, 2011)

Similar Threads

  1. Cant connect with database
    By ronn1e in forum JDBC & Databases
    Replies: 1
    Last Post: January 4th, 2011, 05:45 PM
  2. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 04:09 PM
  3. Problem with accessing result set with postgresql
    By vrp in forum JDBC & Databases
    Replies: 2
    Last Post: December 23rd, 2010, 07:10 AM
  4. Problem with accessing result set with postgresql
    By vrp in forum Member Introductions
    Replies: 2
    Last Post: December 23rd, 2010, 07:10 AM
  5. How to connect keyboard through COM?
    By yogesh01 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 6th, 2010, 05:00 AM