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

📄 booktitlesimpl.java

📁 java web services how to program
💻 JAVA
字号:
// Fig. 11.2: BookTitlesImpl.java.// Class BookTitlesImpl handles the logic for the Book Titles// Web service, which returns a list of book titles that are// stored in a database.package com.deitel.jws1.services;// Java core packagesimport java.io.*;import java.util.*;import java.sql.*;public class BookTitlesImpl {   private Connection connection; // connection to database   // constructor to initialize database connection   public BookTitlesImpl()   {      // load JDBC driver and establish connection to database      try {         // obtain URL of properties file         java.net.URL propertyURL = getClass().getResource(             "BookTitles.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   // service to obtain titles of books listed in database   public String[] getBookTitles()   {      // obtain book titles from database and return title list      try {         String[] bookTitles = null;         // SQL query to database         Statement statement = connection.createStatement(             ResultSet.TYPE_SCROLL_INSENSITIVE,            ResultSet.CONCUR_READ_ONLY );         // use SQL query to obtain book titles from database         ResultSet resultSet =             statement.executeQuery( "SELECT title FROM titles" );         // use ResultSet to create book-title list         if ( resultSet != null ) {            // instantiate book-title list according to number            // of titles stored in ResultSet            List list = new ArrayList();            // store each ResultSet item in book-title list            for ( int i = 0; resultSet.next(); i++ )               list.add( resultSet.getObject( "title" ) );            bookTitles =                ( String[] ) list.toArray( new String[ 0 ] );         }         statement.close();         return bookTitles;      }      // handle exception in executing Statement      catch ( Exception sqlException ) {         sqlException.printStackTrace();         return null;      }   } // end method getBookTitles   // terminate connection to database   protected void finalize()   {      // close database connection      try {         connection.close();      }      // handle if connection cannot be closed      catch ( SQLException sqlException ) {         sqlException.printStackTrace();      }   } // end method finalize} // end class BookTitlesImpl

⌨️ 快捷键说明

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