No suitable driver found for jdbc:postgresql [error]

Started by Jon C.almost 15 years ago5 messagesgeneral
Jump to latest
#1Jon C.
camilleri.jon@gmail.com

1. Upon logging to *SQL shell* the following is being displayed:
*Server [localhost]:*
*Database [postgres]:*
*Port [5432]:*
*Username [postgres]:*
*psql (9.0.4)*
*WARNING: Console code page (437) differs from Windows code page (1252)*
* 8-bit characters might not work correctly. See psql reference*
* page "Notes for Windows users" for details.*
*Type "help" for help.*
*
*
Where do I find the mentioned article?
*
*
2. When running a test program, sourced from Core Java Volume 2 (7th
Edition), the following error is being displayed:
*java.sql.SQLException: No suitable driver found for
jdbc:postgresql:COREJAVA;cre*
*ate=true*
* at java.sql.DriverManager.getConnection(DriverManager.java:602)*
* at java.sql.DriverManager.getConnection(DriverManager.java:185)*
* at TestDB.getConnection(TestDB.java:82)*
* at TestDB.runTest(TestDB.java:43)*
* at TestDB.main(TestDB.java:20)*
*
*
In order to enable JDK to connect to the library files, I copied over *
postgresql-8.4-702.jdbc3.jar*, and,
*postgresql-8.4-702.jdbc4.jar* to *C:\Program Files\Java\jre6\lib\ext*.

It was assumed that the file naming indicates type 3 and type 4
respectively, and, I was intending to use type 4, since it is more efficient
to use a library that translates Java to the database language for Postgre:

*postgresql-8.4-702.jdbc3.jar*

A type 3 driver is a pure Java client library that uses a
database-independent protocol to communicate database requests to a server
component, which then translates the requests into a database-specific
protocol. This can simplify deployment since the database-dependent code is
located only on the server.

*postgresql-8.4-702.jdbc4.jar*

A type 4 driver is a pure Java library that translates JDBC requests
directly to a database-specific protocol.
*TestDB.java*
/**
@version 1.01 2004-09-24
@author Cay Horstmann (all rights reserved)
*/

import java.sql.*;
import java.io.*;
import java.util.*;

/**
This program tests that the database and the JDBC
driver are correctly configured.
*/
class TestDB
{
public static void main (String args[])
{
try
{
runTest();
}
catch (SQLException ex)
{
while (ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}

/**
Runs a test by creating a table, adding a value, showing the table
contents, and
removing the table.
*/
public static void runTest()
throws SQLException, IOException
{
Connection conn = getConnection();
try
{
Statement stat = conn.createStatement();

stat.execute("CREATE TABLE Greetings (Message CHAR(20))");
stat.execute("INSERT INTO Greetings VALUES ('Hello, World!')");

ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
result.next();
System.out.println(result.getString(1));
stat.execute("DROP TABLE Greetings");
}
finally
{
conn.close();
}
}

/**
Gets a connection from the properties specified
in the file database.properties
@return the database connection
*/
public static Connection getConnection()
throws SQLException, IOException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();

String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");

return DriverManager.getConnection(url, username, password);
}
}

*database.properties*
dbc.drivers=org.postgresql.Driver
jdbc.url=jdbc:postgresql:COREJAVA;create=true
jdbc.username=postgre
jdbc.password=

*command line*
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Jon>echo %CLASSPATH%
.;.;C:\PROGRA~1\JMF21~1.1E\lib\sound.jar;C:\PROGRA~1\JMF21~1.1E\lib\jmf.jar;C:\P
ROGRA~1\JMF21~1.1E\lib;C:\Program
Files\Java\external_jars\junit4.9b2\junit4.9b2
\junit-4.9b2.jar;*C:\Program Files\PostgreSQL\pgJDBC;*

C:\Documents and Settings\Jon>cd c:\Program Files\PostgreSQL\pgJDBC

C:\Program Files\PostgreSQL\pgJDBC>dir
Volume in drive C has no label.
Volume Serial Number is D80F-8634

Directory of C:\Program Files\PostgreSQL\pgJDBC

09/07/2011 15:42 <DIR> .
09/07/2011 15:42 <DIR> ..
03/04/2011 23:22 502,118 postgresql-8.4-702.jdbc3.jar
03/04/2011 23:22 539,510 *postgresql-8.4-702.jdbc4.jar*
09/07/2011 15:42 <DIR> scripts
09/07/2011 15:42 5,759,102 uninstall-pgjdbc.exe
3 File(s) 6,800,730 bytes
3 Dir(s) 37,139,398,656 bytes free

C:\Program Files\PostgreSQL\pgJDBC>

How can I get the program running?

--
Jonathan Camilleri

Mobile (MT): 00356 7982 7113
E-mail: camilleri.jon@gmail.com
Please consider your environmental responsibility before printing this
e-mail.

I usually reply to e-mails within 2 business days. If it's urgent, give me
a call.

#2Rodrigo Gonzalez
rjgonzale@estrads.com.ar
In reply to: Jon C. (#1)
Re: No suitable driver found for jdbc:postgresql [error]

On 07/09/2011 12:56 PM, Jonathan Camilleri wrote:

/Notes for Windows users/

http://www.postgresql.org/docs/9.0/static/app-psql.html

#3Jon C.
camilleri.jon@gmail.com
In reply to: Jon C. (#1)
Re: No suitable driver found for jdbc:postgresql [error]

On Jul 9, 6:32 pm, rjgonz...@estrads.com.ar (Rodrigo Gonzalez) wrote:

On 07/09/2011 12:56 PM, Jonathan Camilleri wrote:> /Notes for Windows users/

http://www.postgresql.org/docs/9.0/static/app-psql.html

How does this link answer my questions?

#4Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Jon C. (#3)
Re: No suitable driver found for jdbc:postgresql [error]

On Tuesday, July 12, 2011 6:07:28 am Jon80 wrote:

On Jul 9, 6:32 pm, rjgonz...@estrads.com.ar (Rodrigo Gonzalez) wrote:

On 07/09/2011 12:56 PM, Jonathan Camilleri wrote:> /Notes for Windows
users/

http://www.postgresql.org/docs/9.0/static/app-psql.html

How does this link answer my questions?

It answers your first question on where the "Notes for Windows users" is located.

Your second question is a little more involved. From the error message it looks
like your jdbc driver is no being found. Not sure if it a cut and paste error
but you have:

*database.properties*
dbc.drivers=org.postgresql.Driver
jdbc.url=jdbc:postgresql:COREJAVA;create=true
jdbc.username=postgre
jdbc.password=

The first line should be jdbc.drivers=org.postgresql.Driver .

You might have more luck with an answer on the jdbc list:
http://mail.postgresql.org/mj/mj_wwwusr/domain=postgresql.org?func=lists-long-
full&extra=pgsql-jdbc
--
Adrian Klaver
adrian.klaver@gmail.com

#5Craig Ringer
craig@2ndquadrant.com
In reply to: Jon C. (#1)
Re: No suitable driver found for jdbc:postgresql [error]

On 9/07/2011 11:56 PM, Jonathan Camilleri wrote:

In order to enable JDK to connect to the library files, I copied over
/postgresql-8.4-702.jdbc*3*.jar/, and,
/postgresql-8.4-702.jdbc*4*.jar/ to /C:\Program Files\Java\jre6\lib\ext/.

Argh, don't do that! You're messing with *every* java program on the
system, some of which could have their own bundled copies of different
versions of the PostgreSQL JDBC drivers. Exciting and messy things can
happen.

Just add the JDBC driver to the classpath using the standard "java
-classpath" argument, CLASSPATH env var, or Classpath: jar manifest
entry. See the Java documentation.

When you bundle your app into a .war or .jar for production use, you
typically bundle the JDBC driver within the app jar. See the
documentation on the jar file format.

As for why your program doesn't run even when the driver is on the
classpath: you don't seem to be loading it. In a Java SE environment you
need to force the classloader to find and register a JDBC driver class
before the JDBC DriverManager can find it and use it to handle JDBC
connection URLs. That's usually done with a manual classloader call, like:

Thread.currentThread().getContextClassLoader().loadClass("org.postgresql.Driver");

See the PgJDBC documentation for more detail. Note that the docs use the
old-style static "Class.forName(...)" call, which is fine in simple J2SE
environments but unwise if you start building modular apps, using OSGi
or app servers, etc.

It was assumed that the file naming indicates type 3 and type 4
respectively

"Type 3" and "Type 4" sound like inventions of the author of your
textbook for the purposes of classifying and describing different
approaches to writing drivers - though I could be wrong, of course.

The "3" and "4" in PgJDBC refer to the version of the JDBC spec that
driver is for. JDBC4 drivers can only be used on newer JDKs, so a JDBC3
driver has to be available for people who use older JDKs.

and, I was intending to use type 4, since it is more efficient to use
a library that translates Java to the database language for Postgre:

"Postgres" or "PostgreSQL".

PgJDBC is a "type 4" driver according to that classification scheme,
whether you use the JDBC3 or JDBC4 version.

--
Craig Ringer

POST Newspapers
276 Onslow Rd, Shenton Park
Ph: 08 9381 3088 Fax: 08 9388 2258
ABN: 50 008 917 717
http://www.postnewspapers.com.au/