Server Implementation

The design of the server is simple. The server starts a registry service and registers itself with the registry. It exports methods to add, delete, update, and find records.


Listing 14.2 DatabaseServerImpl.java

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.sql.*;


//server class
public class DatabaseServerImpl extends 
	UnicastRemoteObject implements DatabaseInterface{
	
  //connection to the database;
  private static Connection con = null;

  //static block used to connect to the database
  static
     {
	try{

	//load the driver.
	//Returns the Class object associated 
	//with the class 
	//or interface with the given string name. 
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

	//Create a connection with the database.
	//The DriverManager attempts to select 
	//an appropriate driver from the 
	//set of registered JDBC drivers.
	con = DriverManager.getConnection("jdbc:odbc:students");
	}catch(ClassNotFoundException ex){
	  System.out.println
		("Error connecting : \n " +ex.getMessage());
	
	 //exit if driver is not loaded.
	 System.exit(0);
 	}
	catch(SQLException ex){
	  System.out.println
		("Error connecting : \n " +ex.getMessage());
	}
}

//constructor public DatabaseServerImpl() throws RemoteException,SQLException{ System.out.println("Creating remote object"); } //method used to add a new record. //The method gets a Student object //as its argument. public String addRecord(Student std) throws RemoteException,SQLException { //create a query String query = "INSERT INTO STUDENTINFO VALUES("+ std.getRollNumber() + ",'" + std.getName() + "','"+ std.getCourse()+"')"; //Create a Statement object for sending SQL //statements to the database. Statement stmt=con.createStatement(); //execute the query stmt.executeUpdate(query); return "Record added."; } //method used to update a record. //The method gets a Student object //as its argument. public String updateRecord(Student std) throws RemoteException,SQLException { //create a query String query = "UPDATE STUDENTINFO SET NAME= '"+ std.getName() + "'," + " COURSE = '" + std.getCourse()+ "'"+ " WHERE ROLLNO = " + std.getRollNumber(); //Create a Statement object for sending SQL //statements to the database. Statement stmt = con.createStatement(); //execute query stmt.executeUpdate(query); //return the result return "Record updated successfully."; }
//method used to delete a record. //The method gets the roll number //of the student as parameter. public String deleteRecord(int rollNumber) throws RemoteException,SQLException { //create a query String query= "DELETE FROM STUDENTINFO "+ " WHERE ROLLNO = " + rollNumber; //Create a Statement object for sending SQL //statements to the database. Statement stmt = con.createStatement(); //Returns the number of rows deleted int rows = stmt.executeUpdate(query); if(rows == 0) //if no row is deleted return "No matching record found."; //if record is deleted return "Record deleted."; } //Method used to find a record. The method gets //the roll number of the student as parameter. public Object findRecord(int rollNumber) throws RemoteException,SQLException{ //create a query . String query = "SELECT NAME, COURSE FROM STUDENTINFO"+ " WHERE ROLLNO = " + rollNumber; //Create a Statement object for sending SQL //statements to the database. Statement stmt = con.createStatement(); //Executes an SQL statement that returns a single //ResultSet object. ResultSet rs = stmt.executeQuery(query); //A ResultSet cursor is initially positioned //before the first row; the first call to the method //next makes the first row the current row. if(rs.next()){ //Get the value of the column in the //current row of this ResultSet object as a String String name = rs.getString(1); String course = rs.getString(2); //create a new student object. Student std = new Student(rollNumber,name,course);
//return the object. return std; } //if no matching record is found. return "Record not found."; } //main method public static void main(String[]args){ try{ //server object DatabaseServerImpl obj = new DatabaseServerImpl(); //Create and export a registry on the local host //that accepts requests on port number 1099. Registry reg = LocateRegistry.createRegistry(1099); //bind the server object reg.rebind("DatabaseServer",obj); }catch(Exception e){ System.out.println(e.getMessage()); } } }
 

 

RMI BOOK MAIN PAGE                Top

  
Copyright © 2001 www.universalteacher.com