import java.sql.*;
public class PostgresqlClient{
public static void main(String[] arguments)
throws ClassNotFoundException, SQLException{
String driver = "org.postgresql.Driver";
String url ="localhost";
String user = "root";
String pwd = "password";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, pwd);
System.err.println("Connection complete");
con.close();
}
}
I keep getting the folowing errors:
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged
(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java
:268)
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:164)
at PostgresqlClient.main(PostgresqlClient.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I have tried adding the postgresql.jar file to my CLASSPATH, and have also tried copying the postgresql.jar file to the same directory as the PostgresqlClient.java and PostgresqlClient.class directories. I have also tried running the following from the command line:
java -classpath postgresql.jar PostgresqlClient.class
I am using jdk1.5.0_09 and have tried using nearly all the available postgresql.jar files available.
Any advice would be most appreciated.
Cheers,
Greg Peters....
If you're running from inside NB you need to add the PG driver as a
library to NB. If you have the driver on your classpath then your
class should run fine outside of NB. Test and let us know if it runs
outside of NB or if you have success adding the driver as a library in
NB.
I use Postgresql and it works beautifully. Be encouraged that if you
get the right combo you'll have a superior environment!
Chuck
On 11/25/06, Greg Peters <gregpe...@gmail.com> wrote:
> Hello all,
>
> I'm trying to get the following simple program to run just to confirm a
> connection to the database:
>
> //PostgresqlClient.java
>
> import java.sql.*;
>
> public class PostgresqlClient{
> public static void main(String[] arguments)
> throws ClassNotFoundException, SQLException{
>
> String driver = "org.postgresql.Driver";
> String url ="localhost";
> String user = "root";
> String pwd = "password";
>
> Class.forName(driver);
>
> Connection con = DriverManager.getConnection(url, user, pwd);
> System.err.println("Connection complete");
>
> con.close();
> }
> }
>
>
>
> I keep getting the folowing errors:
>
> Exception in thread "main" java.lang.ClassNotFoundException:
> org.postgresql.Driver
> at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
> 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:164)
> at PostgresqlClient.main(PostgresqlClient.java:12)
> Java Result: 1
> BUILD SUCCESSFUL (total time: 0 seconds)
>
> I have tried adding the postgresql.jar file to my CLASSPATH, and have also
> tried copying the postgresql.jar file to the same directory as the
> PostgresqlClient.java and PostgresqlClient.class directories. I have also
> tried running the following from the command line:
>
> java -classpath postgresql.jar PostgresqlClient.class
>
> I am using jdk1.5.0_09 and have tried using nearly all the available
> postgresql.jar files available.
>
> Any advice would be most appreciated.
>
> Cheers,
>
> Greg Peters....
>
>
---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?
Here are a couple ideas.
*. You can verify that you have a good jar file by running:
jar tf postgresql.jar
- org.postgresql.Driver should be listed in the jar contents.
*. You're launching your app with the wrong command line:
$ java -cp postgresql.jar PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError:
PostgresqlClient/class
Use this instead:
$ java -cp postgresql.jar:. PostgresqlClient
Exception in thread "main" java.sql.SQLException: No suitable
driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at PostgresqlClient.main(PostgresqlClient.java:15)
Finally - that last error is because you're using an incorrectly
formatted URL.
Set
String url = "jdbc:postgresql:littleware://localhost:
5432";
$ javac PostgresqlClient.java
I don't have a 'root' user - but this will hopefully get you a little
closer ...
$ java -cp postgresql.jar:. PostgresqlClient
Exception in thread "main" org.postgresql.util.PSQLException: FATAL:
role "root" does not exist
at
org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages
(ConnectionFactoryImpl.java:443)
at
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl
(ConnectionFactoryImpl.java:98)
at org.postgresql.core.ConnectionFactory.openConnection
(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>
(AbstractJdbc2Connection.java:116)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>
(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3.Jdbc3Connection.<init>
(Jdbc3Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:369)
at org.postgresql.Driver.connect(Driver.java:245)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at PostgresqlClient.main(PostgresqlClient.java:15)
---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match
I believe that in the alternative, you can do a Class.forName("org.postgresql.Driver") in your code before you start accessing the DB. The java.sql code doesn't know WHICH jdbc driver to load when you open the DB until you tell it.
--
Andrew Lazarus dr...@attglobal.net
C:\Documents and Settings\java\GregScratch\src>dir
Volume in drive C has no label.
Volume Serial Number is B468-3FBB
Directory of C:\Documents and Settings\java\GregScratch\src
27/11/2006 06:25 PM <DIR> .
27/11/2006 06:25 PM <DIR> ..
26/11/2006 10:36 PM 726 CheckString.java
24/11/2006 11:51 PM 140 helloworldapp.java
26/11/2006 05:11 PM 402,906 postgresql.jar
27/11/2006 06:25 PM 1,179 PostgresqlClient.class
27/11/2006 06:25 PM 545 PostgresqlClient.java
26/11/2006 02:49 PM 617
SetPoints.java
26/11/2006 01:44 AM 641 VolcanoApplication.java
26/11/2006 01:44 AM 432 VolcanoRobot.java
8 File(s) 407,186 bytes
2 Dir(s) 165,819,129,856 bytes free
C:\Documents and Settings\java\GregScratch\src>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import java.sql.*;
public class PostgresqlClient{
public static void main(String[] Arguments)
throws ClassNotFoundException, SQLException{
String driver = "org.postgresql.Driver";
String url ="jdbc:postgresql:CSG";
String user = "root";
String pwd = "password";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, user, pwd);
System.err.println("Connection complete");
con.close();
}
}
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar. PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar PostgresqlClient.class
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient/class
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar PostgresqlClient
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient
C:\Documents and Settings\java\GregScratch\src>java -cp postgresql.jar:. PostgresqlClient
Exception in thread "main" java.lang.NoClassDefFoundError: PostgresqlClient
C:\Documents and Settings\java\GregScratch\src>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This is what NB says (and I think I have added the module correctly):
Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged (Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java
:268)
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:164)
at PostgresqlClient.main(PostgresqlClient.java:12)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Any ideas????
I have tried several versions of the postgresql module from the website too.
Cheers,
Greg.
---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?
Hi Greg!
Try "java -cp .;postgresql.jar PostgresqlClient"
On Windows the separator char for the classpath is ; not :. And it works
for me only if the . is the first element of the classpath.
PostgresqlClient.class lies in your current directory ("."),
and adding the "." directory to the class path makes java
aware of all classes in the "." "package".
java will not assume you always want to have your current dir
in your classpath, and hence the need for the explicit ".".
>
> Cheers,
>
> Greg
>
> On 11/27/06, Sebastian Esch <es...@in.tum.de> wrote:
> > Hi Greg!
> >
> > Try "java -cp .;postgresql.jar PostgresqlClient"
> >
> > On Windows the separator char for the classpath is ; not :. And it works
> > for me only if the . is the first element of the classpath.
--
Achilleas Mantzios
---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings