Yurttas/PL/DBL/oracle/F/04/03/DBQuery00.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 DBQuery00 {
 20
 21  protected static Connection con;
 22
 23  private static String driverClass = "com.mysql.jdbc.Driver",
 24                        url = "jdbc:mysql:",
 25                        suffix = "//localhost/bank";
 26
 27  public DBQuery00() {}
 28
 29  public DBQuery00(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 mySQL 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 static void main (String[] args) {
 78    String username = args[0];
 79    String password = args[1];
 80
 81    DBQuery00 dbc = new DBQuery00();
 82
 83    try {
 84      if(dbc.connect(username, password)) {
 85        System.out.println("Connected to jdbc:mysql://localhost/bank database...");
 86
 87        Statement statement = con.createStatement();
 88
 89        String queryAll= "SELECT * FROM AA;";
 90
 91        ResultSet rSet = statement.executeQuery(queryAll);
 92
 93        while(rSet.next()) {
 94          String itemA = rSet.getString("itemA");
 95          String itemB = rSet.getString("itemB");
 96          System.out.println(itemA+
 97                             "   "+
 98                             itemB);
 99        }
100
101        if(dbc.disconnect()) 
102          System.out.println("Disconnected to jdbc:mysql://localhost/bank database...");
103      }
104      else System.out.println("Could not connect to jdbc:mysql://localhost/bank database...");
105    }
106    catch(SQLException sqle) {
107      System.err.println("Error accessing to jdbc:mysql://localhost/bank database...");
108      System.err.println(sqle.getMessage());
109    }
110  }
111
112}