Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 5AA921337B80 for ; Wed, 27 Apr 2011 19:07:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 78091-03 for ; Wed, 27 Apr 2011 22:07:43 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 2F295133616B for ; Wed, 27 Apr 2011 19:07:42 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id p3RM7fEP061041 for ; Wed, 27 Apr 2011 19:07:41 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id p3RLd8o0012116 for pgsql-jdbc@postgresql.org; Wed, 27 Apr 2011 18:39:08 -0300 (ADT) (envelope-from news) From: Lew X-Newsgroups: pgsql.interfaces.jdbc 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 Organization: albasani.net Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: news.albasani.net; logging-data="zwHBrERfLT/EXUerBEbfQPP4UklqrvvzcOvkivH0UKtCi6mu+LQ1UpZNgBeTvRAtZtKGchcWW1FVTyYHEB/7cCNJMrGOWwW643QWzGZmBGAtEVYkZiiCbey3iqlAvb1A"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 In-Reply-To: Cancel-Lock: sha1:i7OIzmo4PKKS86Gp2kGCPdUOzy8= To: pgsql-jdbc@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/99 X-Sequence-Number: 21825 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