|
|
|
|
![]() ![]() |
Apr 15 2008, 03:13 AM
Post
#1
|
|
|
Colonel Panic Group: [MODERATOR] Posts: 2,735 Joined: 25-March 05 From: Toronto, Ontario, Canada Member No.: 3,233 |
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 |
|
|
|
Jun 30 2008, 08:41 AM
Post
#2
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
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 |
|
|
|
Jul 7 2008, 12:00 AM
Post
#3
|
|
|
Sparkx Group: [HOSTED] Posts: 343 Joined: 11-October 06 From: Dana Point, CA, USA Member No.: 16,496 |
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 This post has been edited by sparkx: Jul 7 2008, 12:15 AM |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 7th September 2008 - 06:58 PM |