📄 bookorderimpl.java
字号:
// Fig. 7.16: BookOrderImpl.java.
// Class BookOrderImpl is an implementation of the Book Order Web
// service, which enables a client to order a book.
package jws1casestudy.bookstore2;
// Java core packages
import java.io.*;
import java.util.*;
import java.sql.*;
import java.rmi.RemoteException;
// import Java extension packages
import javax.mail.*;
import javax.mail.internet.*;
// Deitel packages
import jws1casestudy.pricefinder.common.Customer;
public class BookOrderImpl implements BookOrder {
private Connection connection; // connection to database
private Properties databaseProperties;
private Properties mailProperties;
// constructor to initialize database connection
public BookOrderImpl() throws Exception
{
InputStream databasePropertyStream = null;
InputStream mailPropertyStream = null;
// load JDBC driver and establish connection to database
try {
// obtain URL of properties file
databasePropertyStream = getClass().getResourceAsStream(
"Database.properties" );
mailPropertyStream =
getClass().getResourceAsStream( "Mail.properties" );
// load properties file
databaseProperties = new Properties();
databaseProperties.load( databasePropertyStream );
mailProperties = new Properties();
mailProperties.load( mailPropertyStream );
// 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();
}
// close properties streams
finally {
// close database property stream
if ( databasePropertyStream != null )
databasePropertyStream.close();
// close mail property stream
if ( mailPropertyStream != null )
mailPropertyStream.close();
}
} // end BookOrderImpl constructor
// service to order book
public void placeOrder( String isbn, Customer customer )
throws RemoteException
{
// ensure valid database connection
if ( connection == null )
throw new RemoteException(
"Unable to establish database connection." );
// insert additional entries for Customer and Order
try {
Statement statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY );
// insert Customer entry in database
statement.executeUpdate( "INSERT INTO Customers " +
"( firstName, lastName, emailAddress, " +
"streetAddress, city, state, zipCode, country, " +
"creditCardNumber ) VALUES ('" +
customer.getFirstName() + "','" +
customer.getLastName() + "','" +
customer.getEmailAddress() + "','" +
customer.getStreetAddress() + "','" +
customer.getCity() + "','" +
customer.getState() + "','" +
customer.getZipCode() + "','" +
customer.getCountry() + "','" +
customer.getCreditCardNumber() + "')" );
// use SQL query to obtain last customerID from database
ResultSet resultCustomerID = statement.executeQuery(
"VALUES ConnectionInfo.lastAutoincrementValue( " +
"'APP', 'CUSTOMERS', 'CUSTOMERID' )" );
resultCustomerID.next();
int customerID = resultCustomerID.getInt( 1 );
// insert Order entry in database
statement.executeUpdate( "INSERT INTO Orders " +
"( customerID ) VALUES ( " + customerID + ")" );
// use SQL query to obtain last orderID from database
ResultSet resultOrderID = statement.executeQuery(
"VALUES ConnectionInfo.lastAutoincrementValue( " +
"'APP', 'ORDERS', 'ORDERID' )" );
resultOrderID.next();
int orderID = resultOrderID.getInt( 1 );
// insert OrderItem entry in database
statement.executeUpdate( "INSERT INTO OrderItems " +
"( ISBN, orderID, quantity ) VALUES ('" +
isbn + "'," + orderID + ",1)" );
statement.close();
// notify success via email
mailConfirmation( customer.getEmailAddress(),
"Confirmation", "Your order has been placed." );
}
// handle exception via email notification
catch ( Exception exception ) {
mailConfirmation( customer.getEmailAddress(),
"Failure", "We were unable to complete your order." );
throw new RemoteException(
"Problem in placeOrder Web-service invocation" );
}
} // end method placeOrder
// send confirmation email to customer
private void mailConfirmation( String email, String subject,
String text )
{
// create email message, then send to customer
try {
// use mail properties to create email message
Message message = new MimeMessage(
Session.getInstance( mailProperties, null ) );
// specify field that indicates who sent the message
message.setFrom( new InternetAddress(
mailProperties.getProperty( "senderAddress" ) ) );
// specify message recipient
message.setRecipient( Message.RecipientType.TO,
new InternetAddress( email ) );
// set message content
message.setSentDate( new java.util.Date() );
message.setSubject( subject );
message.setText( text );
// send email message to recipient
Transport.send( message );
}
// handle exception in sending mail
catch ( Exception exception ) {
exception.printStackTrace();
}
} // end method mailConfirmation
// 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 BookOrderImpl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -