/*
** Demonstrates how to reset the internal statement pointer
*/
import java.sql.*;
import java.io.*;
import java.util.*;

public class DBDemo7
{
  public static void main(String args[])
    throws ClassNotFoundException,SQLException
  {
    Connection con;
    Statement stmt;
    ResultSet result;

    con= connect("dbconnect.txt");   // read connection params & connect
    stmt= con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    stmt.executeUpdate("set search_path to albumdb");

    result= stmt.executeQuery("select * from artist");
    while (result.next()) {
      System.out.println(result.getString("id"));   // field name notation
      System.out.println(result.getString("name"));
      System.out.println(result.getString(3));      // positional notation
    }

	 result.absolute(0);
    while (result.next()) {
      System.out.println(result.getString("id"));   // field name notation
      System.out.println(result.getString("name"));
      System.out.println(result.getString(3));      // positional notation
    }

    con.close();  // close connection to database
  }

  public static Connection connect(String infile)
  {
    Connection con= null;
    String str;

    try {
      Scanner s= new Scanner(new BufferedReader(new FileReader(infile)));
      s.useDelimiter("\n");
      Class.forName(s.next());
      con= DriverManager.getConnection(s.next(),s.next(),s.next());
      s.close();
    }
    catch (FileNotFoundException e) {
      System.err.print("Unable to load database information file: '"+infile+"'\n");
    }
    catch (ClassNotFoundException e) {
      System.err.print("Unable to load database driver\n");
    }
    catch (SQLException e) {
      System.err.print("Unable to connect to the database\n");
    }

    return con;
  }
}

