/*
** Demonstrates obtaining connection parameters from a text file
*/
import java.sql.*;
import java.io.*;
import java.util.*;

public class DBDemo4
{
  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();
    stmt.executeUpdate("set search_path to albumdb,public");

    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
    }
    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;
  }
}

