Databases Reference
In-Depth Information
import java.net.*;
import java.io.*;
import java.sql.*;
import RequestProcessor;
import WriteToFile;
/**
* The FortuneServer object binds to port 8888 and waits for clients to
* connect. When it receives a connection, it interprets this as a request
* for a fortune and starts a RequestProcessor thread to handle the
* request.
* Created October 15, 1998.
* @author Jonathan S. Held
* @version 1.0
* @see RequestProcessor
*/
public class FortuneServer extends Thread {
java.net.ServerSocket fortuneSocket = null;
java.net.Socket clientSocket = null;
java.lang.String url = "jdbc:odbc:Quotation_DB";
java.sql.Connection con = null;
java.sql.Statement stmt = null;
static long numberOfRequests = 0;
final int DB_SIZE = 700, DATA_DUMP = 50;
static int queries[];
/**
* Class constructor
* Creates a socket on port 8888 and binds to it. Attempts to load the
* Sun bridge driver which is used to talk to the MSAccess database.
* Enters into the log file fortune.log the date on which the log file
* entries that follow were created.
* @param none
* @exception ClassNotFoundException thrown if the FortuneServer is
* unable to load the Sun Jdbc-Odbc bridge driver
* @exception SQLException thrown if the database url is unaccessible
* @exception IOException thrown if unable to bind to port 8888 (e.g.,
* the port is already in use by another process)
*/
FortuneServer(){
try {
queries = new int[DB_SIZE];
fortuneSocket = new ServerSocket(8888);
System.runFinalizersOnExit(true);
System.out.println("Fortune server successfully bound to port
8888.");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "sa", "");
stmt = con.createStatement();
System.out.println("Established connection to database.");
System.out.println("Awaiting client requests...");
java.util.Calendar ts = java.util.Calendar.getInstance();
java.lang.String info = new String("Log file created on " +
ts.getTime().toString());
(new WriteToFile(info)).start();
Exhibit 34-8. The
FortuneServer
Java code.
Search WWH ::




Custom Search