Kattare Internet Hosting Home | Home | Services | Contact Us | Order Online |   whitespace image
  Kattare I/S: View FAQ Answer | Webmail | News | FAQ | Members Login |   whitespace image
tail end of the Kattare logo Toll Free: (866) KATTARE -or- (877) KATTARE
Local: (541) 753-1079
keyboard image  whitespace image
You've got Q's? We've got A's.  whitespace image
greenspace image for php hosting, jsp hosting, servlet hosting, java hosting website

Home

Contribute Hosting!

Hosting Services

Java Hosting
  JBoss Hosting
  JSP Hosting
  GlassFish Hosting
  Servlet Hosting
  Tcat Hosting
  Tomcat Hosting

Standard Hosting
  Perl Hosting
  PHP 4 Hosting
  PHP 5 Hosting
  Ruby Hosting

Dedicated Servers
Server Co-Location

Domain Lookup
Join Us Today!

Members

Control Panel
  Ticketing
  Webmail
F.A.Q.
Recent News
Referral System
Spam Blocking

Company

About Us
  History
  Privacy Policy
  Sites We Host
  Strategic Partners
  Terms of Service
  Trademarks
Contact Us
  Phone / Fax
  Mail / Email
  SMS Text


Verify Kattare Internet Services

Documentation

Kattare is dedicated to making your hosting experience as painless as possible. That means making as much help and documentation available as possible. If you have a question that you think would be helpful to have here, please [Submit your Question].

Quick Links
 [Getting Started
 [JSP & Java
 [Ruby & Rails
 [Email & Webmail
Category
JSP-Java Servlets
Question

Last Modified: Mar 18, 2003

How do I use postgres in my java code?
Answer

Importing JDBC

Any source that uses JDBC needs to import the java.sql package, using:

import java.sql.*;

Important: Do not import the org.postgresql package. If you do, your source will not compile, as javac will get confused.

Loading the Driver

Before you can connect to a database, you need to load the driver. There are two methods available, and it depends on your code which is the best one to use.

In the first method, your code implicitly loads the driver using the Class.forName() method. For PostgreSQL, you would use:

Class.forName("org.postgresql.Driver");

This will load the driver, and while loading, the driver will automatically register itself with JDBC.

Note: The forName() method can throw a ClassNotFoundException if the driver is not available.

This is the most common method to use, but restricts your code to use just PostgreSQL. If your code may access another database system in the future, and you do not use any PostgreSQL-specific extensions, then the second method is advisable.

The second method passes the driver as a parameter to the JVM as it starts, using the -D argument. Example:

java -Djdbc.drivers=org.postgresql.Driver example.ImageViewer

In this example, the JVM will attempt to load the driver as part of its initialization. Once done, the ImageViewer is started.

Now, this method is the better one to use because it allows your code to be used with other database packages without recompiling the code. The only thing that would also change is the connection URL, which is covered next.

One last thing: When your code then tries to open a Connection, and you get a No driver available SQLException being thrown, this is probably caused by the driver not being in the class path, or the value in the parameter not being correct.

Connecting to the Database

With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL, this takes one of the following forms:

  • jdbc:postgresql:database
  • jdbc:postgresql://host/database
  • jdbc:postgresql://host:port/database

where:

host

The host name of the server. Defaults to localhost.

port

The port number the server is listening on. Defaults to the PostgreSQL standard port number (5432).

database

The database name.

To connect, you need to get a Connection instance from JDBC. To do this, you would use the DriverManager.getConnection() method:

Connection db = DriverManager.getConnection(url, username, password);

Closing the Connection

To close the database connection, simply call the close() method to the Connection:

db.close();



[FAQ Main] [Submit a Question]

[Back to Category "JSP-Java Servlets"]


whitespace image
green spacer

 [Getting Started]  [JSP & Java]  [Ruby & Rails]  [Email & Webmail]

Kattare Logo
whitespace image
 
whitespace image
whitespace image