Databases Reference
In-Depth Information
import java.net.*;
import java.io.*;
import java.sql.*;
import FortuneServer;
import java.util.Random;
/**
* The RequestProcessor object is used by the FortuneServer to handle
* client requests. This thread is created when the server needs to get a
* quotation or fortune from the MSAccess database, generate five lucky
* numbers, and send the information back to the FortuneClientServlet.
* Created October 15, 1998.
* @author Jonathan S. Held
* @version 1.0
* @see FortuneClientServlet
*/
public class RequestProcessor extends Thread {
java.net.Socket cs = null;
java.sql.Statement statement = null;
final int MAX_FORTUNES = 700;
final int LUCKY_NUMBERS = 5;
final int LOTTERY_NUMBER_MAX_VALUE = 50;
/**
* Class constructor
* @param clientSocket the socket the client attached from
* @exception statement a JDBC Statement object associated with a
* database connection; these parameters are passed from the
* FortuneServer at the time a new RequestProcessor object is created
*/
RequestProcessor(java.net.Socket clientSocket, java.sql.Statement
stmt){
cs = clientSocket;
statement = stmt;
}
/**
* Called when the RequestProcessor thread is started; run generates a
* random number, selects the quotation from the database based on this
* number, then makes creates random numbers; this information is sent
* back to the FortuneClientServlet, which will then process it and
* send it back to the client's browser.
* @param none
* @return void
* @exception IOException thrown if an outputstream cannot be created
* to the client @exception SQLException thrown if an SQL error occurs
* when trying to query the database
*/
public void run(){
try {
Random generator = new Random();
int random = Math.abs(generator.nextInt() % MAX_FORTUNES) + 1;
int num[] = new int[LUCKY_NUMBERS];
java.lang.String query = new String("SELECT * FROM Fortunes WHERE
Fortune_ID = " + random);
FortuneServer.queries[random-1] += 1;
Exhibit 34-9. The RequestProcessor Java code.
Search WWH ::




Custom Search