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.");
}
}
* 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)
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

