⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recording.java

📁 此程序都是企业级 的数据库开发程序 全面揭示了JAVA对数据库的操作
💻 JAVA
字号:
import java.util.*;

import org.jlf.log.*;
import org.jlf.dataMap.*;

/**
 * This class holds information on a recording to help
 * track the recordings available in the stores
 * and track customer purchases and interests.
 */
public class Recording extends DataMappedObject {

  // --------------------------------------------------------------
  // Section 1: DataMappedObject descriptors
  // --------------------------------------------------------------

  // Constants for attributes
  public static final String ID_ATTRIBUTE = "RECORDING_ID";
  public static final String TITLE_ATTRIBUTE = "RECORDING_TITLE";
  public static final String ARTIST_ATTRIBUTE = "RECORDING_ARTIST";
  public static final String CATALOG_NUMBER_ATTRIBUTE = "CATALOG_NUMBER";
  public static final String LIST_PRICE_ATTRIBUTE = "LIST_PRICE";

  // Used for optimistic locking
  public static final String VERSION_ATTRIBUTE = "VERSION";

  // Constants for relationships
  public static final String CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP = 
    "customerTrackedRecordings";

  /**
   * Sets up a table of attribute descriptors for the Recording
   * object.
   */
  protected Hashtable basicGetAttributeDescriptors() {

    // Always call the superclass!
    Hashtable descriptors = super.basicGetAttributeDescriptors();
    DataAttributeDescriptor descriptor;

    // Then add recording-specific attributes
    descriptor = new DataAttributeDescriptor(ID_ATTRIBUTE, 
                                             LongAttribute.class, true);
    descriptor.setIsKeyField(true);
    descriptors.put(ID_ATTRIBUTE, descriptor);

    descriptor = new DataAttributeDescriptor(TITLE_ATTRIBUTE, 
                                             StringAttribute.class, false);
    descriptor.setMaximumLength(50);
    descriptors.put(TITLE_ATTRIBUTE, descriptor);

    descriptor = new DataAttributeDescriptor(ARTIST_ATTRIBUTE, 
                                             StringAttribute.class, false);
    descriptor.setMaximumLength(50);
    descriptors.put(ARTIST_ATTRIBUTE, descriptor);

    descriptor = new DataAttributeDescriptor(CATALOG_NUMBER_ATTRIBUTE, 
                                             StringAttribute.class, false);
    descriptor.setMaximumLength(20);
    descriptors.put(CATALOG_NUMBER_ATTRIBUTE, descriptor);

    descriptor = new DataAttributeDescriptor(LIST_PRICE_ATTRIBUTE, 
                                             DoubleAttribute.class, false);
    descriptors.put(LIST_PRICE_ATTRIBUTE, descriptor);

    descriptor = new DataAttributeDescriptor(VERSION_ATTRIBUTE, 
                                             LongAttribute.class, false);
    descriptors.put(VERSION_ATTRIBUTE, descriptor);

    return descriptors;
  } 

  /**
   * Sets up the relationships from this object to its
   * customer, recording, and tracking type.
   */
  protected Hashtable basicGetRelationshipDescriptors() {

    // Always call the superclass!
    Hashtable descriptors = super.basicGetRelationshipDescriptors();

    // Add new relationships for this class
    descriptors
      .put(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP, 
           new RelationshipDescriptor(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP, 
                                      CustomerTrackedRecording.class));

    return descriptors;

  } 

  /**
   * Default constructor must be public for the data mapping
   * framework!  However, when creating a new object, look to use
   * a paramterized constructor.
   */
  public Recording() {}

  // --------------------------------------------------------------
  // Section 2: Java Bean methods
  // --------------------------------------------------------------

  /**
   * Main constructor for the class.
   */
  public Recording(String recordingTitle, String recordingArtist, 
                   String catalogNumber, double listPrice) {
    setRecordingTitle(recordingTitle);
    setRecordingArtist(recordingArtist);
    setCatalogNumber(catalogNumber);
    setListPrice(listPrice);

    // Set initial object version to 1, increment every time the
    // object is saved
    setVersion(1);
  }

  /**
   * Retrieves the database id of the Recording.
   */
  public long getRecordingId() {
    return getLongAttribute(ID_ATTRIBUTE);
  } 

  /**
   * Retrieves the title of the Recording.
   */
  public String getRecordingTitle() {
    return getStringAttribute(TITLE_ATTRIBUTE);
  } 

  /**
   * Sets the title of the Recording.
   */
  public void setRecordingTitle(String recordingTitle) {
    setStringAttribute(TITLE_ATTRIBUTE, recordingTitle);
  } 

  /**
   * Retrieves the artist that made the Recording.
   */
  public String getRecordingArtist() {
    return getStringAttribute(ARTIST_ATTRIBUTE);
  } 

  /**
   * Sets the artist that made the Recording.
   */
  public void setRecordingArtist(String recordingArtist) {
    setStringAttribute(ARTIST_ATTRIBUTE, recordingArtist);
  } 

  /**
   * Retrieves the catalog number of the Recording.
   */
  public String getCatalogNumber() {
    return getStringAttribute(CATALOG_NUMBER_ATTRIBUTE);
  } 

  /**
   * Sets the catalog number of the Recording.
   */
  public void setCatalogNumber(String catalogNumber) {
    setStringAttribute(CATALOG_NUMBER_ATTRIBUTE, catalogNumber);
  } 

  /**
   * Retrieves the selling price of the Recording.
   */
  public double getListPrice() {
    return getDoubleAttribute(LIST_PRICE_ATTRIBUTE);
  } 

  /**
   * Sets the selling price of the Recording.
   */
  public void setListPrice(double listPrice) {
    setDoubleAttribute(LIST_PRICE_ATTRIBUTE, listPrice);
  } 

  /**
   * Retrieves the version of the Recording object.
   */
  public long getVersion() {
    return getLongAttribute(VERSION_ATTRIBUTE);
  } 

  /**
   * Sets the version of the Recording object.
   */
  private void setVersion(long version) {
    setLongAttribute(VERSION_ATTRIBUTE, version);
  } 


  /**
   * Retrieves the CustomerTrackedRecording objects
   * related to this object.
   */
  public Vector getCustomerTrackedRecordings() {
    return getRelatedObjects(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP);
  } 

  /**
   * Adds a CustomerTrackedRecording object
   * related to this object.
   */
  public void addCustomerTrackedRecording(CustomerTrackedRecording ctr) {
    addRelatedObject(CUSTOMER_TRACKED_RECORDINGS_RELATIONSHIP, ctr);
  } 


  // --------------------------------------------------------------
  // Section 3: Methods to map the class to the database
  // --------------------------------------------------------------

  /**
   * Finds zero to many Recording objects that have a title
   * like the input string, returning them in a Vector.
   */
  public static Vector findByTitle(String likeRecordingTitle) {

    // Create a new recording and populate the title to look for
    Recording recording = new Recording();
    recording.setRecordingTitle(likeRecordingTitle);

    // Have the framework execute the query
    return recording.findManyBySearchCriteria("findByTitle");
  } 

  /**
   * Finds zero to many Recording objects that have an artist
   * like the input string, returning them in a Vector.
   */
  public static Vector findByArtist(String likeRecordingArtist) {

    // Create a new recording and populate the title to look for
    Recording recording = new Recording();
    recording.setRecordingArtist(likeRecordingArtist);

    // Have the framework execute the query
    return recording.findManyBySearchCriteria("findByArtist");
  } 

  // --------------------------------------------------------------
  // Section 4: Methods to make the class self-contained
  // --------------------------------------------------------------

  /**
   * Prints the contents of all variables.
   */
  public String toString() {
    return "Recording: " + getRecordingId() + "\n  Title: " 
           + getRecordingTitle() + "\n  Artist: " + getRecordingArtist() 
           + "\n  Catalog Number: " + getCatalogNumber() 
           + "\n  List Price: " + getListPrice();
  } 

  /**
   * Helper method to output a set of Recording objects
   * in a Vector to System.out.
   */
  private static void outputRecordings(Vector recordings) {

    Recording recording;
    Enumeration e = recordings.elements();

    while (e.hasMoreElements()) {
      recording = (Recording) e.nextElement();

      // Print to output stream
      System.out.println("\n" + recording);
    } 

  } 

  /**
   * Main method to test the capabilities above.  CRUDs the
   * database by creating 4 recordings, reading them back,
   * updates the price on one and reads it back, and
   * deletes all 4.
   */
  public static void main(String[] args) {
    Recording rec1, rec2, rec3, rec4;

    DataMapper dataMapper = null;

    try {
      rec1 = new Recording("Cuts Like A Knife", "Bryan Adams", 
                           "392-0000022", 11.99);

      rec2 = new Recording("Working Class Dog", "Rick Springfield", 
                           "RS-32-1133", 10.99);

      rec3 = new Recording("Come And Join Us", "Petra", "303-9388293", 
                           11.99);

      rec4 = new Recording("Lucky Town", "Bruce Springsteen", 
                           "BK-30-23993", 13.99);

      dataMapper = rec1.getDefaultDataMapper();

      // Create the recordings in the database
      rec1.write(dataMapper);
      rec2.write(dataMapper);
      rec3.write(dataMapper);
      rec4.write(dataMapper);

      dataMapper.commitWrites();

      // Read the recordings back and output
      System.out.println("\nYou should see 4 recordings:");

      Vector springArtistRecordings = Recording.findByArtist("%Spring%");
      outputRecordings(springArtistRecordings);

      Vector recordingsStartWithC = Recording.findByTitle("C%");
      outputRecordings(recordingsStartWithC);

      // Update recording 1 price
      rec1.setListPrice(7.99);
      rec1.write(dataMapper);
      dataMapper.commitWrites();

      // Read record back and make sure update made it
      System.out.println("\nThis recording should have a price of 7.99");
      Vector updatedRecording = Recording.findByArtist("Bryan Adams");
      outputRecordings(updatedRecording);

      // Delete all recordings
      rec1.deleteOnWrite();
      rec1.write(dataMapper);

      rec2.deleteOnWrite();
      rec2.write(dataMapper);

      rec3.deleteOnWrite();
      rec3.write(dataMapper);

      rec4.deleteOnWrite();
      rec4.write(dataMapper);

      dataMapper.commitWrites();

      // Read all recordings back, shouldn't be any
      System.out.println("\nDeleted all, shouldn't be any recordings:");
      Vector allRecordings = Recording.findByArtist("%");
      outputRecordings(allRecordings);

    } catch (Exception e) {
      e.printStackTrace();
    } 
    finally {
      if (dataMapper != null) {
        dataMapper.close();
      } 
    } 
  } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -