Nov 22, 2009

JDBC With Postgresql - Can't find org.postgresql.Driver??

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > Java

JDBC With Postgresql - Can't find org.postgresql.Driver??

xboxrulz
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.

CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author 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:
QUOTE
init:
deps-jar:
Compiling 1 source file to D:\devel\java\netbeans\PostrgreSQLTEST\build\classes
compile-single:
run-single:
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:


help?

xboxrulz

 

 

 


Comment/Reply (w/o sign-up)

FeedBacker
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

Comment/Reply (w/o sign-up)

sparkx
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

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for jdbc, postgresql
advertisement



JDBC With Postgresql - Can't find org.postgresql.Driver??

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com