HOME       >>       Programming

JDBC With Postgresql Cant find org.postgresql.Driver??


So recently I have been trying to connect a PostgreSQL database that I have set up on my local Windows Vista machine (yes, Windows machine, I know it's horrible for servers; still working on getting the darn thing to work on my MacOS X on my laptop). So I initialized the service, and now it's running. However, the problem is that I tried to connect the database using the code below I found on the website where the @author is now. Obviously I had to edit the code so it would work and connect to my server and database.


/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author http://http://www.faqs.org/docs/ppbook/x20856.htm */import java.sql.DriverManager;import java.sql.Connection;import java.sql.SQLException;public class Test1 { public static void main(String[] argv) { System.out.println("Checking if Driver is registered with DriverManager."); try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException cnfe) { System.out.println("Couldn't find the driver!"); System.out.println("Let's print a stack trace, and exit."); cnfe.printStackTrace(); System.exit(1); } System.out.println("Registered the driver ok, so let's make a connection."); Connection c = null; try { // The second and third arguments are the username and password, // respectively. They should be whatever is necessary to connect // to the database. c = DriverManager.getConnection("jdbc:postgresql://localhost/JTest", "postgres", "main"); } catch (SQLException se) { System.out.println("Couldn't connect: print out a stack trace and exit."); se.printStackTrace(); System.exit(1); } if (c != null) System.out.println("Hooray! We connected to the database!"); else System.out.println("We should never get here."); }}

Running this code gives me this error:



Compiling 1 source file to D:\devel\java\netbeans\PostrgreSQLTEST\build\classes



java.lang.ClassNotFoundException: org.postgresql.Driver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

Checking if Driver is registered with DriverManager.

Couldn't find the driver!

Let's print a stack trace, and exit.

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:169)

at Test1.main(Test1.java:19)

Java Result: 1

BUILD SUCCESSFUL (total time: 1 second)

I'm running this via Netbeans 6.0.1 on JDK 1.6.0_05 x64 with PostgreSQL Server 8.3.1. It works with Netbeans but not the code as explained in the image below:






JDBC With Postgresql - Can

JDBC With Postgresql


Replying to xboxrulz




Hi this is gourav garg, You must have to add the library file of postgres to load the driver's . I did it . If You ant to library then you can contact em on my email




-reply by gourav garg


I don't use NetBeans because in my opinion it is just easier to use Notepad (or other editors if you’re on linux) and batch files rather then learning NetBeans. I have used MySQL on windows and received a similar error. It occurs when the jar "org" is not present in the running folder. For example your class is in C:/.../My Documents/my_class.class. The class org.postgresql.Driver located in either a folder named org or org.jar needs to be moved to C:/.../My Documents/. If you upload my_class.class then you need to also upload org.jar because it is required to run the applet.This should solve your problem.Good Luck,Sparkx


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.