On 9/07/2011 11:56 PM, Jonathan Camilleri wrote:
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.
"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. "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/ |