From: Lew <noone@lewscanon.com>
To: pgsql-jdbc@postgresql.org
Subject: Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801]
Date: Wed, 27 Apr 2011 17:39:23 -0400
Message-ID: <ipa2ds$p4b$1@news.albasani.net> (raw)
In-Reply-To: <BANLkTi=VpazmODCUShc5a+WNaf4uspf5fA@mail.gmail.com>
References: <BANLkTi=vNEroPFa159HUw9DcxWuj=yHSwA@mail.gmail.com>
<BANLkTi=VpazmODCUShc5a+WNaf4uspf5fA@mail.gmail.com>
Brady S Edwards wrote:
> try {
> System.out.println("Trying to connect");
> Class.forName ("org.postgresql.Driver");
> Properties props = new Properties();
> props.setProperty("user", PG_USER);
> props.setProperty("password", PG_PASS);
> Connection pgConn = DriverManager.getConnection(PG_CONN_PROP, props);
> System.out.println("Looks like connections succeeded\n");
> PreparedStatement pStmt = pgConn.prepareStatement(
> "INSERT INTO soo (zoo) values (?)");
This code makes me do a doubletake, and not just because of the ridiculously
wide TAB-based indentation. Why reload the driver class over and over again?
Only the first time does anything.
One might wish to take the Sun/Oracle advice:
"The DriverManager methods getConnection and getDrivers have been enhanced to
support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers
must include the file META-INF/services/java.sql.Driver. This file contains
the name of the JDBC drivers implementation of java.sql.Driver. For example,
to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file
would contain the entry:
my.sql.Driver
[org.postgresql.Driver]
"Applications no longer need to explictly load JDBC drivers using
Class.forName(). Existing programs which currently load JDBC drivers using
Class.forName() will continue to work without modification."
You still don't need 'Class.forName()' more than once per driver if you stick
to the old way.
Personally I prefer JNDI-based approaches to loading DB drivers. I also
usually prefer JPA to raw JDBC, but there are times when JDBC is
unquestionably the better tool.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: noone@lewscanon.com
Subject: Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801]
In-Reply-To: <ipa2ds$p4b$1@news.albasani.net>
References: <BANLkTi=vNEroPFa159HUw9DcxWuj=yHSwA@mail.gmail.com> <BANLkTi=VpazmODCUShc5a+WNaf4uspf5fA@mail.gmail.com> <ipa2ds$p4b$1@news.albasani.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox