pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801]
3+ messages / 2 participants
[nested] [flat]

* Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801]
@ 2011-04-27 21:39  Lew <noone@lewscanon.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Lew @ 2011-04-27 21:39 UTC (permalink / raw)
  To: pgsql-jdbc

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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801]
@ 2011-04-27 22:44  Brady S Edwards <brady.s.edwards@seagate.com>
  parent: Lew <noone@lewscanon.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Brady S Edwards @ 2011-04-27 22:44 UTC (permalink / raw)
  To: Lew <noone@lewscanon.com>; +Cc: pgsql-jdbc

So, I was just going for a simple quick illustration of the problem
that I was running into. I had a meeting and spent 5 minutes typing up
a very simple example.

The actual code uses tomcat's JNDI based connection pooling which works fine.
For the indent, my tabstop is set to 3 in my editor instead of 8, I
should have expanded the tab's I guess.
The insert statement works.
The trying to prepare an anonymous plpgsql block with a ? (in between
the $$ $$) does not work, the reason for which was helpfully pointed
out by someone else.

Your response does have anything to do with the question I was asking.

I was hoping to find out how (if it's possible) to execute something
like in postgres:
PreparedStatement pStmt = conn.prepareStatement("DECLARE oh
varchar(10); BEGIN oh := ?; END;");
pStmt.setString(1, "Blah");


On Wed, Apr 27, 2011 at 3:39 PM, Lew <noone@lewscanon.com> wrote:
> 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
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>



-- 
Brady Edwards
(720) 684-2984
Seagate Technology



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801]
@ 2011-04-28 02:10  Lew <noone@lewscanon.com>
  parent: Brady S Edwards <brady.s.edwards@seagate.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Lew @ 2011-04-28 02:10 UTC (permalink / raw)
  To: pgsql-jdbc

Brady S Edwards wrote:
> Your response does have anything to do with the question I was asking.

I had noted that your question was already answered.  This is a discussion 
group, not a helpdesk.  I was discussing.  Oh, please, please pardon me.  I 
guess I'm not your little slave lackey-boy.

I notice that your response didn't have anything to do with the points I 
raised, which were on topic for a JDBC newsgroup.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2011-04-28 02:10 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-04-27 21:39 Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801] Lew <noone@lewscanon.com>
2011-04-27 22:44 ` Brady S Edwards <brady.s.edwards@seagate.com>
2011-04-28 02:10   ` Lew <noone@lewscanon.com>

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