import java.io.*;

import java.sql.*;



public class MageSetup {

  // If rerunning, add drop.txt as first filename to array.

  static String[] files = {"comments.txt", "instructors.txt", "students.txt"};



  public static void main (String args[]) {

    String driverPrefixURL = "jdbc:odbc:";

    String username = null;

    String password = null;

    String dataSource = null;

    if (args.length != 3) {

      System.out.println ("Usage: java MageSetup ODBCDriverName username password");

      return;

    } else {

      dataSource = args[0];

      username = args[1];

      password = args[2];

    }

    try {

      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    } catch (Exception e) {

      System.out.println("Failed to load JDBC/ODBC driver.");

      return;

    }

    try {

      Connection con = DriverManager.getConnection(driverPrefixURL+dataSource, username, password);

      System.out.println("Connected.");

      Statement s = con.createStatement();

      String line;

      for (int i=0;i<files.length;i++) {

        System.out.println ("Processing: " + files[i]);

        BufferedReader in = new BufferedReader (new FileReader(files[i]));

        while ((line = in.readLine()) != null) {

          System.out.println ("Executing: " + line);

          s.executeUpdate(line);

        }

        in.close();

      }

      s.close();

      con.close();

    }catch (Exception e) {

      e.printStackTrace();

    }

  }

}