Yurttas/PL/DBL/mysql-f-2010/02/00/01/04/DBMeta01.java

  1/**
  2 *  Copyright(C) 2005
  3 *  All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4 *
  5 *  Permission to use, copy, modify, and distribute this
  6 *  software and its documentation for EDUCATIONAL purposes
  7 *  and without fee is hereby granted provided that this
  8 *  copyright notice appears in all copies.
  9 *
 10 *  @date   : February 2005, April 2010.
 11 *  @author : Salih Yurttas.
 12 */
 13
 14
 15import dbops.DBConnect;
 16import dbops.DBTable;
 17
 18import java.sql.*;
 19import java.net.*;
 20
 21import java.util.*;
 22import java.io.*;
 23
 24public class DBMeta01 {
 25
 26  protected static Connection con;
 27
 28  public static void main (String[] args)
 29    throws IOException, SQLException
 30  {
 31    String username = args[0];
 32    String password = args[1];
 33
 34    DBConnect dbC = new DBConnect();
 35
 36    String outText = "jdbc:mysql://database2.cse.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database...";
 37
 38    try {
 39      if(dbC.connect(args[0], args[1])) {
 40        System.out.println("Connected to " + outText);
 41        System.out.println();
 42
 43        con = dbC.getConnect();
 44
 45        DBTable dbT = new DBTable(con);
 46
 47        String inputFilename = getString("query");
 48        String query = getData(inputFilename);
 49
 50        System.out.println();
 51
 52        ResultSet rSet = dbT.getStatement(query);
 53
 54        ArrayList<String> tables = dbT.getTableNames(query);
 55        int n = tables.size();
 56        System.out.println("Bank Tables :");
 57        for(int i=0; i<n; ++i)
 58          System.out.println((String)tables.get(i));
 59
 60        System.out.println();
 61
 62        if(dbC.disconnect()) 
 63          System.out.println("Disconnected from " + outText);
 64      }
 65      else System.out.println("Could not connect to " + outText);
 66    }
 67    catch(SQLException sqle) {
 68      System.out.println();
 69      System.err.println("Error accessing " + outText);
 70      System.err.println(sqle.getMessage());
 71      System.out.println();
 72    }
 73  }
 74
 75  public static String getString(final String message)
 76    throws IOException
 77  {
 78    DataInputStream dIS = new DataInputStream(System.in);
 79    Reader kR = new BufferedReader(new InputStreamReader(dIS));
 80    StreamTokenizer kTokens = new StreamTokenizer(kR);
 81
 82    System.out.println();
 83    System.out.print("-> " + message + " : ");
 84    System.out.flush();
 85    kTokens.nextToken();
 86    String data = kTokens.sval;
 87
 88    return data;
 89  }
 90
 91  public static String getData(final String filename)
 92    throws IOException
 93  {
 94    BufferedReader fR = new BufferedReader(new FileReader(filename));
 95
 96    String line = null;
 97    String data = "";
 98
 99    while((line=fR.readLine())!=null)
100      data += line;
101
102    return data;
103  }
104
105}