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

📄 ticketrequestbean2.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
/*
 * TicketRequestBean2.java
 *
 * Created on July 9, 2002, 11:22 AM
 */

package com.samspublishing.jpp.ch23;

import java.sql.*;
import java.util.*;

/**
 *
 * @author  Stephen Potts
 * @version
 */
public class TicketRequestBean2 implements java.io.Serializable
{
   //information about the customer
   private int custID;
   private String lastName;
   private String firstName;
   
   //information about the cruise
   private int cruiseID;
   private String destination;
   private String port;
   private String sailing;
   
   private int numberOfTickets;
  
   public TicketRequestBean2()
   {
   }
   
   /** Constructor */
   public TicketRequestBean2(int custID, String lastName, String firstName, int cruiseID, String destination, String port, String sailing, int numberOfTickets, boolean isCommissionable)
   {
      //set the information about the customer
      this.custID = custID;
      this.lastName = lastName;
      this.firstName = firstName;
      
      //set the information about the cruise
      this.cruiseID = cruiseID;
      this.destination = destination;
      this.port = port;
      this.sailing = sailing;
      
      this.numberOfTickets = numberOfTickets;
   }
   
   public int getCustID()
   {
      return this.custID = custID;
   }
   
   public String getLastName()
   {
      return this.lastName = lastName;
   }
   
   public String getFirstName()
   {
      return this.firstName = firstName;
   }
   
   public int getCruiseID()
   {
      return this.cruiseID;
   }
   
   public String getDestination()
   {
      return this.destination;
   }
   
   public String getPort()
   {
      return this.port;
   }
   
   public String getSailing()
   {
      return this.sailing;
   }
   
   public int getNumberOfTickets()
   {
      return this.numberOfTickets;
   }
   
   
   public void setCustID(int custID)
   {
      this.custID = custID;
   }
   
   public void setLastName(String lastName)
   {
      this.lastName = lastName;
   }
   
   public void setFirstName(String firstName)
   {
      this.firstName = firstName;
   }
   
   public void setCruiseID(int cruiseID)
   {
      this.cruiseID = cruiseID;
   }
   
   public void setDestination(String destination)
   {
      this.destination = destination;
   }
   
   public void setPort(String port)
   {
      this.port = port;
   }
   
   public void setSailing(String sailing)
   {
      this.sailing = sailing;
   }
   
   public void setNumberOfTickets(int numberOfTickets)
   {
      this.numberOfTickets = numberOfTickets;
   }
      
   public String toString()
   {
      String outString;
      outString = "-------------------------------------------" + "\n";
      
      //information about the customer
      outString += "custID = " + this.custID + "\n";
      outString += "lastName = " + this.lastName + "\n";
      outString += "firstName = " + this.firstName + "\n";
      outString += "-----------------------------------------" + "\n";
      
      //information about the cruise
      outString += "cruiseID = " + this.cruiseID + "\n";
      outString += "destination = " + this.destination + "\n";
      outString += "port = " + this.port + "\n";
      outString += "sailing = " + this.sailing + "\n";
      outString += "numberOfTickets = " + this.numberOfTickets + "\n";
      
      outString += "-----------------------------------------" + "\n";
      return outString;
   }
   
   public String updateDB()
   {
      java.sql.Connection dbConn = null;
      Statement statement1 = null;
      String createStatement;
      String insertStatement;
      
      try
      {
         // ============== Make connection to database ==================
         
         //load the driver class
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         
         //Specify the ODBC data source
         String sourceURL = "jdbc:odbc:TicketRequest";
         
         //get a connection to the database
         dbConn = DriverManager.getConnection(sourceURL);
         
         //If we get to here, no exception was thrown
         System.out.println("The database connection is " + dbConn);
         System.out.println("Making connection...\n");
         
         //Create the statement
         statement1 = dbConn.createStatement();
         
         ////////////////////////////////////////////////////////////////////////////////
         //                       Create the tables in the database                    //
         ////////////////////////////////////////////////////////////////////////////////
                 
         insertStatement = "INSERT INTO TicketRequest VALUES(" +
         custID + "," +
         "'" + lastName + "'," +
         "'" + firstName + "'," +
         cruiseID + "," +
         "'" + destination + "'," +
         "'" + port + "'," +
         "'" + sailing + "',"  +
         numberOfTickets + ")";
         System.out.println(insertStatement);
         statement1.executeUpdate(insertStatement);
         return "Successful Update";
      } catch (Exception e)
      {
         System.out.println("Exception was thrown: " + e.getMessage());
         return "UnSuccessful Update";
      } finally
      {
         try
         {
            if (statement1 != null)
               statement1.close();
            if (dbConn != null)
               dbConn.close();
         } catch (SQLException sqle)
         {
            System.out.println("SQLException during close(): " + sqle.getMessage());
         }
      }
      
   }
   
   public String retrieveFromDB()
   {
      java.sql.Connection dbConn = null;
      Statement statement1 = null;
      String createStatement;
      String insertStatement;
      
      try
      {
         // ============== Make connection to database ==================
         
         //load the driver class
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         
         //Specify the ODBC data source
         String sourceURL = "jdbc:odbc:TicketRequest";
         
         //get a connection to the database
         dbConn = DriverManager.getConnection(sourceURL);
         
         //If we get to here, no exception was thrown
         System.out.println("The database connection is " + dbConn);
         System.out.println("Making connection...\n");
         
         //Create the statement
         statement1 = dbConn.createStatement();
         
         ////////////////////////////////////////////////////////////////////////////////
         //                       Create the tables in the database                    //
         ////////////////////////////////////////////////////////////////////////////////
          
        //Populate the bean
        String getBeanString =
        "SELECT * FROM TicketRequest " +
        "WHERE CustID = " + custID;
        
        ResultSet results = statement1.executeQuery(getBeanString);
        
        while (results.next())
        {
             lastName = results.getString("lastName");
             firstName = results.getString("firstName");
             cruiseID = results.getInt("cruiseID");
             destination = results.getString("destination");
             port = results.getString("port");
             sailing = results.getString("sailing");
             numberOfTickets = results.getInt("numberOfTickets");
        }
        return "Successful Retrieval";
      } catch (Exception e)
      {
         System.out.println("Exception was thrown: " + e.getMessage());
         return "UnSuccessful Retrieval";
      } finally
      {
         try
         {
            if (statement1 != null)
               statement1.close();
            if (dbConn != null)
               dbConn.close();
         } catch (SQLException sqle)
         {
            System.out.println("SQLException during close(): " + sqle.getMessage());
         }
      }
      
   }    
}

⌨️ 快捷键说明

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