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

📄 bookpriceimpl.java

📁 java web services how to program
💻 JAVA
字号:
// Fig. 7.11: BookPriceImpl.java.
// Class BookPriceImpl is an implementation of the Book Price Web 
// service, which returns the price of a book, based on a 
// specified ISBN.
package jws1casestudy.bookstore2;

// Java core packages
import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.NumberFormat;
import java.rmi.*;

// Deitel packages
import jws1casestudy.pricefinder.common.PriceQuote;

public class BookPriceImpl implements BookPrice {

   private Connection connection; // connection to database
   private Properties databaseProperties;
   private Properties storeProperties;

   // constructor to initialize database connection
   public BookPriceImpl() throws Exception
   {
      InputStream databasePropertyStream = null; 
      InputStream storePropertyStream = null;

      // load JDBC driver and establish connection to database
      try {

         // obtain URLs of properties files
         databasePropertyStream = 
            getClass().getResourceAsStream( 
               "Database.properties" );
         storePropertyStream = 
            getClass().getResourceAsStream( 
               "BookStore.properties" );

         // load properties files
         databaseProperties = new Properties();
         databaseProperties.load( databasePropertyStream );
         storeProperties = new Properties();
         storeProperties.load( storePropertyStream );

         // load JDBC driver
         Class.forName( databaseProperties.getProperty( 
            "jdbcDriver" ) );

         // establish database connection
         connection = DriverManager.getConnection(
            databaseProperties.getProperty( "databaseURI" ) );
      }

      // handle exception if database driver does not exist
      catch ( ClassNotFoundException classNotFoundException ) {
         classNotFoundException.printStackTrace();
         throw new Exception( "Unable to initialize service" );
      }

      // handle exception in making Connection
      catch ( SQLException sqlException ) {
         sqlException.printStackTrace();
         throw new Exception( "Unable to initialize service" );
      }

      // handle exception in loading properties file
      catch ( IOException ioException ) {
         ioException.printStackTrace();
         throw new Exception( "Unable to initialize service" );
      }

      // close properties streams
      finally {

         // close database property stream
          if ( databasePropertyStream != null )
             databasePropertyStream.close();

          // close bookstore property stream
          if ( storePropertyStream != null )
             storePropertyStream.close();
      }
   } // end BookPriceImpl constructor

   // service to obtain price of books based on ISBN
   public PriceQuote getPrice( String ISBN )
      throws RemoteException
   {
      // ensure valid database connection
      if ( connection == null )
         throw new RemoteException( 
            "Unable to establish database connection" );

      // query database for price associated with ISBN
      try {

         // SQL query to database
         Statement statement = connection.createStatement(); 

         // use SQL query to obtain price from database
         ResultSet resultSet = statement.executeQuery( 
            "SELECT price FROM Books WHERE ISBN = " + ISBN );

         PriceQuote priceQuote = null;

         // extract price from ResultSet
         if ( resultSet != null ) {
            resultSet.next();

            // format price to two decimal places
            NumberFormat numberFormat = 
               NumberFormat.getNumberInstance(); 
            numberFormat.setMaximumFractionDigits( 2 );
            double price = Double.parseDouble( 
               numberFormat.format( 
                  resultSet.getDouble( "price" ) ) ); 

            // get store ID from properties file
            int storeID = Integer.parseInt( 
               storeProperties.getProperty( "storeID" ) );

            // get store description from properties file
            String storeDescription = 
               storeProperties.getProperty( "storeDescription" );

            // create PriceQuote from ResultSet and properties
            priceQuote = new PriceQuote();
            priceQuote.setPrice( price );
            priceQuote.setIsbn( ISBN );
            priceQuote.setStoreID( storeID );
            priceQuote.setStoreDescription( storeDescription );
         }
         statement.close();

         return priceQuote;
      }

      // handle exception in executing Statement
      catch ( SQLException sqlException ) {
         sqlException.printStackTrace();
         throw new RemoteException(
            "Error occured in BookPrice Web-service invocation" );
      }

   } // end method getPrice

   // close database connection
   public void finalize()
   {
      // close database connection
      try {
         if ( connection != null )
            connection.close();
      }

      // handle expection in closing database
      catch ( SQLException sqlException ) {
         sqlException.printStackTrace();
      }

   } // end method finalize

} // end class BookPriceImpl

⌨️ 快捷键说明

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