import java.sql.*;

public class TestKeys {
	static public void main(String[] args) {
		if (args.length > 0) {
			String table = args[0];
			Connection conn = null;
			try {
				Class.forName("org.postgresql.Driver");
				conn = DriverManager.getConnection("jdbc:postgresql://firewall/photoalbum", "postgres", "");
				DatabaseMetaData dbmd = conn.getMetaData();
				
				System.out.println("Imported keys:");
				ResultSet rs = dbmd.getImportedKeys(null, null, table);
				while (rs.next()) {
					System.out.println("PKTABLE_NAME = " + rs.getString("PKTABLE_NAME"));
					System.out.println("PKCOLUMN_NAME = " + rs.getString("PKCOLUMN_NAME"));
					System.out.println("FKTABLE_NAME = " + rs.getString("FKTABLE_NAME"));
					System.out.println("FKCOLUMN_NAME = " + rs.getString("FKCOLUMN_NAME"));
				}
				rs.close();
				
				System.out.println("Exported keys:");
				rs = dbmd.getExportedKeys(null, null, table);
				while (rs.next()) {
					System.out.println("PKTABLE_NAME = " + rs.getString("PKTABLE_NAME"));
					System.out.println("PKCOLUMN_NAME = " + rs.getString("PKCOLUMN_NAME"));
					System.out.println("FKTABLE_NAME = " + rs.getString("FKTABLE_NAME"));
					System.out.println("FKCOLUMN_NAME = " + rs.getString("FKCOLUMN_NAME"));
				}
				rs.close();
				conn.close();
			} catch (Exception ex) {
				ex.printStackTrace(System.err);
			}
			System.exit(0);
		} else {
			System.err.println("Please specify a tablename");
		}
	}
}
