Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 108BC1337B92 for ; Wed, 27 Apr 2011 19:45:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 19542-04 for ; Wed, 27 Apr 2011 22:45:14 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mx0a-000cda01.pphosted.com (mx0a-00003501.pphosted.com [67.231.144.15]) by mail.postgresql.org (Postfix) with ESMTP id 168EA1337B61 for ; Wed, 27 Apr 2011 19:45:13 -0300 (ADT) Received: from pps.filterd (m0004002 [127.0.0.1]) by mx0a-000cda01.pphosted.com (8.14.4/8.14.4) with SMTP id p3RMj7RM021002 for ; Wed, 27 Apr 2011 18:45:12 -0400 Received: from mail-iy0-f170.google.com (mail-iy0-f170.google.com [209.85.210.170]) by mx0a-000cda01.pphosted.com with ESMTP id vxuud86sw-4 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NOT) for ; Wed, 27 Apr 2011 18:45:12 -0400 Received: by mail-iy0-f170.google.com with SMTP id 12so2218789iyb.15 for ; Wed, 27 Apr 2011 15:45:12 -0700 (PDT) Received: by 10.43.57.211 with SMTP id wh19mr3623920icb.1.1303944312150; Wed, 27 Apr 2011 15:45:12 -0700 (PDT) MIME-Version: 1.0 Received: by 10.231.19.196 with HTTP; Wed, 27 Apr 2011 15:44:52 -0700 (PDT) In-Reply-To: References: From: Brady S Edwards Date: Wed, 27 Apr 2011 16:44:52 -0600 Message-ID: Subject: Re: O/T: Class.forName(driver) repeatedly? [Was: JDBC parameter binding not working for PL/PGSQL block JDBC3 Version 9.0-801] To: Lew Cc: pgsql-jdbc@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Proofpoint-PolicyRoute: Outbound X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.2.15, 1.0.148, 0.0.0000 definitions=2011-04-27_08:2011-04-27, 2011-04-27, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=2 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=6.0.2-1012030000 definitions=main-1104270160 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201104/100 X-Sequence-Number: 21826 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 fin= e. 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 =3D conn.prepareStatement("DECLARE oh varchar(10); BEGIN oh :=3D ?; END;"); pStmt.setString(1, "Blah"); On Wed, Apr 27, 2011 at 3:39 PM, Lew wrote: > Brady S Edwards wrote: >> >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0try { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0System.out.println("Trying to connect"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0Class.forName ("org.postgresql.Driver"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0Properties props =3D new Properties(); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0props.setProperty("user", PG_USER); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0props.setProperty("password", PG_PASS); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0Connection pgConn =3D >> DriverManager.getConnection(PG_CONN_PROP, props); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0System.out.println("Looks like connections >> succeeded\n"); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0PreparedStatement pStmt =3D pgConn.prepareStatement( >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"INSERT INTO soo (zoo) values (?)"= ); > > This code makes me do a doubletake, and not just because of the ridiculou= sly > wide TAB-based indentation. =C2=A0Why reload the driver class over and ov= er > again? =C2=A0Only the first time does anything. > > One might wish to take the Sun/Oracle advice: > "The DriverManager methods getConnection and getDrivers have been enhance= d > to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 > Drivers must include the file META-INF/services/java.sql.Driver. This fil= e > 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: > > =C2=A0my.sql.Driver > > =C2=A0[org.postgresql.Driver] > > "Applications no longer need to explictly load JDBC drivers using > Class.forName(). Existing programs which currently load JDBC drivers usin= g > 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. =C2=A0I = 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 > --=20 Brady Edwards (720) 684-2984 Seagate Technology