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

📄 voteimpl.java

📁 java web services how to program
💻 JAVA
字号:
// VoteImpl.java
// VoteImpl implements the Vote remote interface to provide
// a VoteService remote object.
package com.deitel.jws1.jaxrpc.service.vote;

// Java core packages
import java.rmi.*;
import java.sql.*;

// Java XML packages
import javax.xml.rpc.server.*;
import javax.xml.rpc.JAXRPCException;
import javax.servlet.ServletContext;

public class VoteImpl implements ServiceLifecycle, Vote {

   private Connection connection;
   private PreparedStatement sqlUpdate, sqlSelect;
   
   // set up database connection and prepare SQL statement
   public void init( Object context )
      throws JAXRPCException
   {
      // attempt database connection and 
      // create PreparedStatements
      try {

         // cast context to ServletEndpointContext
         ServletEndpointContext endpointContext = 
            ( ServletEndpointContext ) context;

         // get ServletContext
         ServletContext servletContext = 
            endpointContext.getServletContext();

         // get database driver from servlet context
         String dbDriver = 
            servletContext.getInitParameter( "dbDriver" );
   
         // get database name from servlet context
         String voteDB = 
            servletContext.getInitParameter( "voteDB" );

         // load database driver
         Class.forName( dbDriver );

         // connect to database
         connection = DriverManager.getConnection( voteDB );

         // PreparedStatement to increment vote total for a
         // specific language
         sqlUpdate = 
            connection.prepareStatement(
               "UPDATE surveyresults SET vote = vote + 1 " +
               "WHERE name = ?"
            );

         // PreparedStatement to obtain surveyresults table's data
         sqlSelect =
            connection.prepareStatement( "SELECT name, vote " +
               "FROM surveyresults ORDER BY vote DESC"
            );

      } // end try

      // for any exception, throw an JAXRPCException to
      // indicate that the servlet is not currently available
      catch ( Exception exception ) {
         exception.printStackTrace();

         throw new JAXRPCException( exception.getMessage() );
      }

   } // end method init

   // implementation for interface Vote method addVote
   public String addVote( String name ) throws RemoteException
   {
      // get votes count from database and update it
      try {

         // set parameter in sqlUpdate
         sqlUpdate.setString( 1, name );

         // execute sqlUpdate statement
         sqlUpdate.executeUpdate();

         // execute sqlSelect statement
         ResultSet results = sqlSelect.executeQuery();
         StringBuffer voteInfo = new StringBuffer();

         // iterate ResultSet and prepare return string
         while ( results.next() ) {

            // append results to String voteInfo
            voteInfo.append( " " + results.getString( 1 ) );
            voteInfo.append( " " + results.getInt( 2 ) );
         }

         return voteInfo.toString();

      } // end try

      // handle exceptions during database connection
      catch ( Exception exception ) {
         exception.printStackTrace();

         return exception.getMessage();
      }

   } // end method addVote

   // close SQL statements and database when servlet terminates
   public void destroy()
   {
      // attempt to close statements and database connection
      try {
         sqlUpdate.close();
         sqlSelect.close();
         connection.close();
      }

      // handle database exception
      catch ( Exception exception ) {
         exception.printStackTrace();
      }

   } // end method destroy

} // end class VoteImpl


/**************************************************************************
 * (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

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