Yurttas/PL/DBL/mssql/F/04/00/01/DBDrop00.java

From ZCubes Wiki
Jump to navigation Jump to search
  1/**
  2 *  Copyright(C) 2004
  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   : January 1, 2004.
 11 *  @author : Salih Yurttas.
 12 */
 13
 14
 15import java.sql.*;
 16import java.net.*;
 17
 18public class DBDrop00 {
 19
 20  protected Connection con;
 21
 22  private static String driverClass = "com.mysql.jdbc.Driver",
 23                        url = "jdbc:mysql:",
 24                        suffix = "//localhost/bank";
 25
 26  public DBDrop00() {}
 27
 28  public DBDrop00(String jdbcDriver,
 29                  String dbURL,
 30                  String urlSuffix) { 
 31    setDBInfo(jdbcDriver,
 32              dbURL,
 33              urlSuffix);
 34  }
 35
 36  public static void setDBInfo(String jdbcDriver,
 37                               String dbURL,
 38                               String urlSuffix) { 
 39    driverClass = jdbcDriver;
 40    url = dbURL;
 41    suffix = urlSuffix;
 42  }
 43
 44  public boolean connect(String username,
 45                         String password) 
 46    throws SQLException
 47  {
 48    try {
 49      Class.forName(driverClass);
 50      con = DriverManager.getConnection(url+suffix,
 51                                        username,
 52                                        password);
 53      if(con.isClosed()) return false;
 54    }
 55    catch(ClassNotFoundException cnfe) {
 56      throw new SQLException("The mySQL JDBC Driver could not be loaded.");
 57    }
 58
 59    return true;
 60  }
 61
 62  public boolean disconnect(Connection con) 
 63    throws SQLException
 64  {
 65    con.close();
 66    return true;
 67  }
 68
 69  public boolean disconnect()
 70    throws SQLException
 71  {
 72    con.close();
 73    return true;
 74  }
 75
 76  public boolean setStatement(String query) 
 77    throws SQLException
 78  {
 79    Statement st = con.createStatement();
 80    st.executeUpdate(query);
 81    return true;
 82  }
 83
 84  public static void main (String[] args) {
 85    String username = args[0];
 86    String password = args[1];
 87
 88    DBDrop00 dbc = new DBDrop00();
 89
 90    try {
 91      if(dbc.connect(args[0], args[1])) {
 92        System.out.println("Connected to jdbc:mysql://localhost/bank database...");
 93
 94        String dropTable = "DROP TABLE AA;";
 95
 96        if(dbc.setStatement(dropTable))
 97          System.out.println("Table AA is dropped from jdbc:mysql://localhost/bank database...");
 98
 99        if(dbc.disconnect()) 
100          System.out.println("Disconnected from jdbc:mysql://localhost/bank database.");
101      }
102      else System.out.println("Could not connect to jdbc:mysql://localhost/bank database.");
103    }
104    catch(SQLException sqle) {
105      System.err.println("Error accessing jdbc:mysql://localhost/bank database.");
106      System.err.println(sqle.getMessage());
107    }
108  }
109
110}