Sei sulla pagina 1di 3

Establishing a Connection in Postgresql

Load the Driver in Eclipse Importing the driver: 1. Right click on your project 2. Choose property 3. Choose Java build path 4. Choose add external JARS.. and select the location to the JDBC driver. Establish a connection with the DBMS you want to use. Establishing a connection involves two steps: Loading the driver, and making the connection. Loading the Driver Loading the driver you want to use is very simple. It involves just one line of code in your program. To use the Java DB driver, add the following line of code:
Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection ("jdbc:postgresql://localhost:5432/exerdb","exerdb", "exerdb"); /* JDBC URL, User, Password */

Calling the Class.forName automatically creates an instance of a driver and registers it with the DriverManager, so you don't need to create an instance of the class. After you have loaded a driver, it can make a connection with a DBMS. Making the Connection The second step in establishing a connection is to have the appropriate driver connect to the DBMS. Using the DriverManager Class The DriverManager class works with the Driver interface to manage the set of drivers available to a JDBC client. When the client requests a connection and provides a URL, the DriverManager is responsible for finding a driver that recognizes the URL and for using it to connect to the corresponding data source. Connection URLs have the following form: jdbc:<dbName>[propertyList] The dbName portion of the URL identifies a specific database. A database can be in one of

many locations: in the current working directory, on the classpath, in a JAR file, in a specific Java DB database home directory, or in an absolute location on your file system. In Postgresql the URL can be any of any of the following formats : jdbc:postgresql:database jdbc:postgresql://host/database jdbc:postgresql://host:port/database The getConnection method establishes a connection: Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbase" ,"myLogin", "myPassword");

In place of " myLogin " you insert the name you use to log in to the DBMS; in place of " myPassword " you insert your password for the DBMS. So, if you log in to your DBMS with a login name of "Juan " and a password of " J1 ", just these two lines of code will

String url = "jdbc:postgresql://localhost:5432/dbase" Connection conn = DriverManager.getConnection(url, "Fernanda","J1");

SAMPLE PROGRAM USING POSTGRESQL


package pbook;

import java.sql.*; public class dbpbook {

public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try{ // Load JDBC Driver (specify Driver class name) // Establish connection to DBMS Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection("jdbc:postgresql://localhost:" + "5432/exerdb","exerdb", "exerdb"); /* JDBC URL, User, Password */ // Create statement stmt = conn.createStatement(); // Execute SQL rs = stmt.executeQuery("SELECT * FROM account"); while(rs.next()) { // Display current line System.out.print( rs.getString(1) + "\t"); System.out.print(rs.getString(2) + "\t"); System.out.print(rs.getString(3) + "\t"); System.out.println(); } } catch(SQLException e) { System.out.println("An exception has occurred."); e.printStackTrace(); } catch(ClassNotFoundException ex){ System.out.println("Failed to load Driver"); ex.printStackTrace(); // Display exception details } finally { try{ // Close respectively if( stmt != null ){ stmt.close(); } if( conn != null ){ conn.close(); } if( rs != null ){ rs.close(); } } catch(Exception e){ e.printStackTrace(); }

} }

Potrebbero piacerti anche