up previous next contents
Next: JUnit Up: Databases and JDBC Previous: PreparedStatements   Contents

A real method

The following method was copied from a production software application. Note that it uses a PreparedStatement for the syntactical ease of use that the PreparedStatement offers.

  public String getPurchaserEmailAddress(int orderId)
  throws SQLException
  {
    Connection connection = null;
    try
    {
      String email = null;
      connection = ConnectionPool.getConnection();
      String query = "SELECT email FROM orders WHERE order_id = ?";
      PreparedStatement emailQuery = connection.prepareStatement(query);
      emailQuery.setInt(1, orderId);
      ResultSet rs = emailQuery.executeQuery();
      if ( rs.next() )
      {
        email = rs.getString(1);
      }
      return email;
    }
    finally
    {
      ConnectionPool.freeConnection(connection);
    }
  }


Next: JUnit Up: Databases and JDBC Previous: PreparedStatements   Contents