Yurttas/PL/DBL/postgres/F/02/00/01/03/DBQuery00.java

From ZCubes Wiki
Revision as of 00:02, 5 November 2013 by MassBot1 (talk | contribs) (Created page with "<syntaxhighlight lang="java" line start="1" enclose="div">/** * Copyright(C) 2005 * All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc.. * * P...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
  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 1, 2005.
 11 *  @author : Salih Yurttas.
 12 */
 13
 14
 15import java.sql.*;
 16import java.net.*;
 17
 18public class DBQuery00 {
 19
 20  protected static Connection con;
 21
 22  private static String driverClass = "com.mysql.jdbc.Driver",
 23                        url = "jdbc:mysql:",
 24                        suffix = "//database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank";
 25
 26  public DBQuery00() {}
 27
 28  public DBQuery00(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 static void main (String[] args) {
 77    String username = args[0];
 78    String password = args[1];
 79
 80    DBQuery00 dbc = new DBQuery00();
 81
 82    try {
 83      if(dbc.connect(username, password)) {
 84        System.out.println("Connected to jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database...");
 85
 86        Statement statement = con.createStatement();
 87
 88        String queryAll= "SELECT * FROM AA;";
 89
 90        ResultSet rSet = statement.executeQuery(queryAll);
 91
 92        while(rSet.next()) {
 93          String itemA = rSet.getString("itemA");
 94          String itemB = rSet.getString("itemB");
 95          System.out.println(itemA+
 96                             "   "+
 97                             itemB);
 98        }
 99
100        if(dbc.disconnect()) 
101          System.out.println("Disconnected to jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database...");
102      }
103      else System.out.println("Could not connect to jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database...");
104    }
105    catch(SQLException sqle) {
106      System.err.println("Error accessing to jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database...");
107      System.err.println(sqle.getMessage());
108    }
109  }
110
111}