Csc 1302, Honors Principles of Computer Science II (Fall 2021)

Week 3 (10 September 2021)

Initialize Database from file

During class, you will complete coding of the initializeDatabase method. The pseudo code is given below. You will handin Database.java by end of class time.
  1. Initialize Database (Database.java)
    //Create the database object by reading data from several files in directory dir
    public void initializeDatabase(String dir) {
      //Pseudo Code follows
      Open file "catalog.dat"
      Read number of relations in the database
      for each relation {
        // Read relation name and schema information
        Read relation name
        Read number of attributes un the relation
        Create empty array lists for attributes and domain
        for each attribute {
          Read attribute name
          Read attribute domain
          Add attribute name to attributes array list
          Add domain to domain array list
        }
        Create a new Relation object
        // Now Read data for tuples, create tuples and add to relation
        Construct file name for relation data file
        Open the relation data file
        Read number of tuples in relation
        for each tuple {
          Create new tuple object
          for each component of tuple {
            Read component value and add to tuple
          }
          Add tuple to relation
        }
        Close relation data file
        Add relation to database
      }
      Close catalog.dat file
    }
    
    Here are sample files for various databases:
    1. drinks (drinks.tar)
    2. mdb (mdb.tar)
    3. movie (movie.tar)
    4. rs (rs.tar)
    5. sps (sps.tar)

    To read data from a file, you can use the following code:

    FileInputStream fin1 = null;
    BufferedReader infile1 = null;
    try {
      fin1 = new FileInputStream("f.dat");
      infile1 = new BufferedReader(new InputStreamReader(fin1));
      String s = infile1.readLine();
      ...
      ...
      fin1.close();
    } catch (IOException e) {
        System.out.println("Error reading file");
      }