📄 bookinformationimpl.java
字号:
// BookInformationImpl.java
// BookInformationImpl is the implementation of the Deitel
// BookInformation Web service, which provides detailed
// information for various Deitel publications.
package jws1casestudy.deitelbookinformation;
// Java core packages
import java.rmi.RemoteException;
import java.sql.*;
// Deitel packages
import jws1casestudy.pricefinder.common.BookDetails;
public class BookInformationImpl implements BookInformation
{
// PreparedStatement for obtaining detailed book information
private PreparedStatement bookDetailsStatement;
private Connection connection;
// BookInformationImpl constructor
public BookInformationImpl()
{
try {
// load database driver
Class.forName( "COM.cloudscape.core.RmiJdbcDriver" );
// connect to database
connection = DriverManager.getConnection(
"jdbc:cloudscape:rmi:deitelbooks" );
// created PreparedStatement for obtaining book details
bookDetailsStatement = connection.prepareStatement(
"SELECT * FROM titles WHERE isbn = ?" );
}
// handle exception connecting to database
catch ( Exception exception ) {
exception.printStackTrace();
}
}
// retrieve detailed information about the book with the
// given ISBN and return a BookDetails object
public BookDetails getBookDetails( String ISBN )
throws Exception
{
try {
// set the ISBN for the PreparedStatement and execute
bookDetailsStatement.setString( 1, ISBN );
ResultSet results = bookDetailsStatement.executeQuery();
// check for empty ResultSet
if ( !results.next() )
throw new Exception( "Book Not Found" );
// populate BookDetails object with data from ResultSet
BookDetails details = new BookDetails();
details.setIsbn( results.getString( "isbn" ) );
details.setTitle( results.getString( "title" ) );
details.setAuthors( results.getString( "authors" ) );
details.setDescription(
results.getString( "description" ) );
details.setCoverImageURL(
results.getString( "coverImageURL" ) );
return details;
} // end try block
// handle exception retrieving book information
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
// throw exception to remote caller
throw new RemoteException(
"Error accessing database", sqlException );
}
} // end method getBookDetails
// close database resources before garbage collection occurs
public void finalize()
{
try {
if ( bookDetailsStatement != null )
bookDetailsStatement.close();
if ( connection != null )
connection.close();
}
// handle exception closing database resources
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -