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