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

📄 recording.java

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

/**
 * This class connects to an Oracle database with a simple
 * Recording table and maps the data in this class to/from
 * that table.  One instance of this class corresponds to one
 * row in the ODBC_RECORDINGS table.
 */
public class Recording {

  // --------------------------------------------------------------
  // Section 1: Java Bean methods
  // --------------------------------------------------------------

  /**
   * Holds the synthetic primary key of the ODBC_RECORDINGS table.
   */
  private long recordingId;

  /**
   * Holds the title of the Recording.
   */
  private String recordingTitle;

  /**
   * Holds the artist that made the Recording.
   */
  private String recordingArtist;

  /**
   * Holds the catalog number of the Recording for ordering.
   */
  private String catalogNumber;

  /**
   * Holds the selling price of the Recording.
   */
  private double listPrice;
  private String idSQL;

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

  /**
   * Retrieves the database ID of the Recording.
   */
  public long getRecordingId() {
    return recordingId;
  } 

  /**
   * Sets the database ID of the Recording.
   */
  private void setRecordingId(long recordingId) {
    this.recordingId = recordingId;
  } 

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

  /**
   * Sets the title of the Recording.
   */
  public void setRecordingTitle(String recordingTitle) {
    this.recordingTitle = recordingTitle;
  } 

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

  /**
   * Sets the artist that made the Recording.
   */
  public void setRecordingArtist(String recordingArtist) {
    this.recordingArtist = recordingArtist;
  } 

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

  /**
   * Sets the catalog number of the Recording.
   */
  public void setCatalogNumber(String catalogNumber) {
    this.catalogNumber = catalogNumber;
  } 

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

  /**
   * Sets the selling price of the Recording.
   */
  public void setListPrice(double listPrice) {
    this.listPrice = listPrice;
  } 



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

  /**
   * Helper method to iterate through a result set and create
   * one Recording object for each row, returning them in a Vector.
   */
  private static Vector resultSetToRecordings(ResultSet resultSet) 
          throws SQLException {
    Vector recordings = new Vector();
    Recording recording;

    while (resultSet.next()) {

      // Retrieve values from the resultset
      long recordingId = resultSet.getLong(1);
      String recordingTitle = resultSet.getString(2);
      String recordingArtist = resultSet.getString(3);
      String catalogNumber = resultSet.getString(4);
      double listPrice = resultSet.getDouble(5);

      // Construct and populate all variables
      recording = new Recording(recordingTitle, recordingArtist, 
                                catalogNumber, listPrice);
      recording.setRecordingId(recordingId);

      // Add to output vector
      recordings.addElement(recording);
    } 

    return recordings;
  } 

  /**
   * Helper method to find zero to many Recording objects given
   * a SQL query, returning them in a Vector.
   */
  private static Vector findRecordingsBySQLQuery(String sqlQuery) 
          throws Exception {

    Connection databaseConnection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {

      // Get a new connection and statement
      databaseConnection = getConnection();
      statement = databaseConnection.createStatement();

      // Run the query
      resultSet = statement.executeQuery(sqlQuery);

      // Translate the result set into Recording objects
      return resultSetToRecordings(resultSet);

    } finally {

      // If we got a result set, close it
      if (resultSet != null) {
        resultSet.close();
      } 

      // If we got a statement, close it
      if (statement != null) {
        statement.close();
      } 

      // If we got a connection, close it
      if (databaseConnection != null) {
        databaseConnection.close();
      } 
    } 
  } 

  /**
   * 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) 
          throws Exception {
    String sqlQuery = "select * from RECORDINGS " +
                      "where RECORDING_TITLE like '" + likeRecordingTitle +
                      "'";

    return findRecordingsBySQLQuery(sqlQuery);
  } 

  /**
   * 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) 
          throws Exception {
    String sqlQuery = "select * from RECORDINGS " +
                      "where RECORDING_ARTIST like '" +
                      likeRecordingArtist + "'";

    return findRecordingsBySQLQuery(sqlQuery);
  } 


  /**
   * Inserts a new Recording object in the database.  Uses an
   * Oracle sequence to get a unique identifier for the table key.
   */
  public void create() throws Exception {

    Connection databaseConnection = null;
    Statement statement = null;
    Statement statement2 = null;
    ResultSet resultSet = null;

    try {

      // Get a new connection and statements
      databaseConnection = getConnection();

      // Set up a transaction for the two SQL statements
      databaseConnection.setAutoCommit(false);

      statement = databaseConnection.createStatement();
      statement2 = databaseConnection.createStatement();

      // Insert the row
      String insertSQL = "insert into RECORDINGS ( "  +
                         " RECORDING_TITLE, " + " RECORDING_ARTIST, " +
                         " CATALOG_NUMBER, " + " LIST_PRICE) " +
                         "values ( " + "'" +
                         getRecordingTitle() + "', " + "'" +
                         getRecordingArtist() + "', " + "'" +
                         getCatalogNumber() + "', " + getListPrice() +
                         ") ";

      statement.executeUpdate(insertSQL);

      // execute query to get recordingId
      
      idSQL = "select max(RECORDING_ID) from RECORDINGS";
      resultSet = statement2.executeQuery(idSQL);

      // retrieve from result set
      resultSet.next();

⌨️ 快捷键说明

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