/*
 * Sample program for accessing a Postgres database from Java.
 *
 * Note: This program needs a PostgreSQL JDBC driver to be in the classpath.
 */

package iis;

import java.sql.*;

public class HelloPostgres {
	public static void main(String[] args) {
		// Load the JDBC driver
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			System.err.println("ERROR: Unable to load driver");
			e.printStackTrace();
			return;
		}

		// Connect to the database server
		Connection db;
		try {
			db = DriverManager.getConnection("jdbc:postgresql://lsir-cis-pcY:54XX/actors", "userXX", "password");
		} catch (SQLException e1) {
			System.err.println("ERROR: Unable to connect to database server");
			e1.printStackTrace();
			return;
		}

		// Query the database and show the results
		try {
			Statement st = db.createStatement();
			ResultSet rs = st.executeQuery("SELECT ID, Name FROM Actors WHERE Name Like 'M%' ORDER BY Name");
			while (rs.next())
				System.out.println(rs.getString(1) + "\t" + rs.getString(2));
			rs.close();
			st.close();
		} catch (SQLException e2) {
			System.err.println("ERROR: An error occurred while querying the database");
			e2.printStackTrace();
		}
	}
}

