Skip to main content

A taste of "Bitter Java"

How antipatterns can improve your programming

Return to sidebar

package bbs;

// These programs are from Bitter Java.
// (c) 2001, Bruce Tate.
// Keep in mind that they are from a book describing
//    the wrong way to code.  Use at your own risk.


import java.io.*;
import java.sql.*;
import COM.ibm.db2.*;
import java.util.*;


/**
* Creation date: (07/17/2001 5:07:55 PM)
*/
public class PostListCommand {

  // Field indexes for command properties	
  private static final int SUBJECT_COLUMN = 1;
  private static final int AUTHOR_COLUMN = 2;
  private static final int BOARD_COLUMN = 3;
  protected Vector author = new Vector();
  protected Vector subject = new Vector();
  protected Vector board = new Vector();

  // SQL result set
  protected ResultSet result;
  protected Connection connection = null;

  /**
   * execute
   * This is the work horse method for the command.
   * It will execute the query and get the result set.
   */
  public void execute()
    throws
      java.lang.Exception,
      java.io.IOException,
      com.ibm.db.DataException {

    try {
      // retrieve data from the database
      Statement statement = connection.createStatement();
      result =
        statement.executeQuery("SELECT subject, author, board from posts");
      while (result.next()) {
        subject.addElement(result.getString(SUBJECT_COLUMN));
        author.addElement(result.getString(AUTHOR_COLUMN));
        board.addElement(result.getString(BOARD_COLUMN));
      }
      result.close();
      statement.close();
    } catch (Throwable theException) {
      theException.printStackTrace();
    }

  }
  /**
   * getAuthor
   * This method will get the author property.
   * Since the SQL statement returns a result set,
   * we will index the result.
   */
  public String getAuthor(int index)
    throws
      java.lang.IndexOutOfBoundsException,
      java.lang.ArrayIndexOutOfBoundsException {
    return (String) author.elementAt(index);
  }
  /**
   * getBoard
   * This method will get the board property.
   * Since the SQL statement returns a result set,
   * we will index the result.
   */
  public String getBoard(int index)
    throws
      java.lang.IndexOutOfBoundsException,
      java.lang.ArrayIndexOutOfBoundsException {
    return (String) board.elementAt(index);
  }
  /**
   * getSubject
   * This method will get the subject property.
   * Since the SQL statement returns a result set,
   * we will index the result.
   */
  public String getSubject(int index)
    throws
      java.lang.IndexOutOfBoundsException,
      java.lang.ArrayIndexOutOfBoundsException {
    return (String) subject.elementAt(index);
    ;
  }
  /**
   * initialize
   * This method will connect to the database.
   */
  public void initialize()
    throws java.io.IOException, com.ibm.db.DataException {

    try {
      Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();

      // URL is jdbc:db2:dbname
      String url = "jdbc:db2:board";

      // connect with default id/password
      connection = DriverManager.getConnection(url);

    } catch (Throwable theException) {
      theException.printStackTrace();
    }
  }

  /**
   * Insert the method's description here.
   * Creation date: (07/17/2001 11:38:44 PM)
   * @return int
   * @exception IndexOutOfBoundsException
   */
  public int getSize() {
    return author.size();
  }
}

Return to sidebar