bookorderimpl.java

来自「java web services how to program」· Java 代码 · 共 111 行

JAVA
111
字号
// Fig. 15.17: BookOrderImpl.java// Class BookOrderImpl is the implementation of the BookOrder// Web service, which determines the price of a book, based on// that book's ISBN and the quantity of books to order.package com.deitel.jws1.services;// Java core packagesimport java.io.*;import java.util.*;import java.sql.*;public class BookOrderImpl    implements com.deitel.jws1.services.BookOrder {   private Connection connection; // connection to database   // constructor to initialize database connection   public BookOrderImpl()   {      // load JDBC driver and establish connection to database      try {         // obtain URL of properties file         java.net.URL propertyURL = getClass().getResource(             "bookStock.properties" );         // load properties file         Properties databaseProperties = new Properties();         databaseProperties.load( new FileInputStream(             propertyURL.getPath() ) );         // 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();      }      // handle exception in making Connection      catch ( SQLException sqlException ) {         sqlException.printStackTrace();      }      // handle exception in loading properties file      catch ( IOException ioException ) {         ioException.printStackTrace();      }   } // end constructor   // obtain price of book based on book's ISBN and quantity   public double orderBook( String ISBN, int quantity )   {      // detemine book availability, then determine price      try {         // SQL query to database         Statement statement = connection.createStatement(             ResultSet.TYPE_SCROLL_INSENSITIVE,            ResultSet.CONCUR_READ_ONLY );         // make query to determine number of available books         ResultSet resultSet = statement.executeQuery( "SELECT" +            " quantity, price FROM books WHERE isbn = " + ISBN );         int availableBookCount = 0;         // obtain quantity associated with isbn from database          if ( resultSet != null ) {            resultSet.next();            availableBookCount = resultSet.getInt( "quantity" );         }         // determine whether quantity exceeds number of          // available books in database         if ( availableBookCount < quantity ) {            statement.close();            return -1;         }         else {            // determine price for one book            double pricePerBook = resultSet.getDouble( "price" );            int newQuantity = availableBookCount - quantity;            // update database to decrement number of books            statement.execute( "UPDATE books " +               "SET quantity = " + newQuantity +                " WHERE isbn = " + ISBN );            statement.close();            return pricePerBook * quantity;         }      }      // handle exception in executing Statement      catch ( SQLException sqlException ) {         sqlException.printStackTrace();         return -1;      }   } // end method orderBook   } // end class BookPriceImpl

⌨️ 快捷键说明

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