// JDBC Example - printing a database's metadata
// Coded by Chen Li/Kirill Petrov Winter, 2005
// Slightly revised for ICS185 Spring 2005, by Norman Jacobson
// Slightly revised for CS122B Spring 2007, by Norman Jacobson


import java.sql.*;				// Enable SQL processing

public class JDBCex1
{

	public static void main(String[] arg) throws Exception
	{

		// Incorporate postgreSQL driver
		Class.forName("org.postgresql.Driver").newInstance();

		// Connect to the test database
		Connection connection =
			DriverManager.getConnection("jdbc:postgresql://localhost/moviedb", "testuser", "testpass");

		// Create an execute an SQL statement to select all of table"Stars" records
		Statement select = connection.createStatement();
		ResultSet result = select.executeQuery("Select * from stars");

		// Get metatdata from stars; print # of attributes in table
		System.out.println("The results of the query");
		ResultSetMetaData metadata = result.getMetaData();
		System.out.println("There are " + metadata.getColumnCount() + " columns");

		// Print type of each attribute
		for (int i = 1; i <= metadata.getColumnCount(); i++)
			System.out.println("Type of column "+ i + " is " + metadata.getColumnTypeName(i));

		// print table's contents, field by field
		while (result.next())
		{
			System.out.println("Id = " + result.getInt(1));
			System.out.println("Name = " + result.getString(2) + result.getString(3));
			System.out.println("DOB = " + result.getString(4));
			System.out.println("photoURL = " + result.getString(5));
			System.out.println();
		}
	}
}
