Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Csy3020 JDBC Rmi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

RMI based Chat Server & Client

Using MySQL
The following client and server RMI programs implement a simple chat system. The
system is essentially a modification of the earlier RMI chat client and server examples
where the main change is that the messages are stored and retrieved from a MySQL
database using JDBC.

The database is on turtle.eng.nene.ac.uk (194.81.104.22) and server must be hosted


on www.eng.nene.ac.uk (194.81.104.27). The database is espen and the table within
the database is message. The table message consists of just one field, which is messg.
The following screen dump shows the creation of the table message. To connect to
the server using PuTTY and enter at the unix prompt

sh-2.05$ /usr/bin/mysql -u www -p


Enter password:

or from the dos prompt enter

C:\ > mysql –h 194.81.104.22 –u www –p


Enter password:

You will then be presented with the MySQL prompt

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 18630137 to server version: 4.0.12

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use espen;


Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table message (messg varchar (100));
Query OK, 0 rows affected (0.00 sec)

mysql> describe message;


+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| messg | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql>
RMIJDBCChatClient.java

This client RMI chat client program is essentially the same as the earlier RMI chat
client with only the name of the classes for the client and server having changed
(There are some minor changes as to how the messages are displayed. This is merely
cosmetic).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.rmi.*;

public class RMIJDBCChatClient extends SwingChatGUI


{
public ButtonHandler bHandler;
RMIJDBCChatServer s = null;

public RMIJDBCChatClient (String title)


{
super (title);
bHandler = new ButtonHandler();
sendButton.addActionListener( bHandler );

try
{
s = (RMIJDBCChatServer) Naming.lookup
("//194.81.104.27/RMIJDBCChatServer");
}
catch (NotBoundException n) {}
catch (MalformedURLException m){}
catch (RemoteException m){}
}

private class ButtonHandler implements ActionListener


{
public void actionPerformed (ActionEvent event)
{
String message;

try
{
message = txArea.getText ();
s.putMessage (message);
System.out.println ("Message: " + message);
}
catch (RemoteException i) {}
}
}

public void run ()


{
String message = "";
while (true)
{
try
{
message = s.getMessage (0);
rxArea.setText (message);
}
catch (RemoteException i) {}
}
}

public static void main (String[] args) //throws IOException


{
RMIJDBCChatClient f = new RMIJDBCChatClient
("RMI & JDBC based Chat Client Program");

f.pack ();
f.show ();
f.run ();
}
}

The client can be run on any machine. However, it needs to be compiled with the
stub files, which are generated when the server is compiled. It is possible to compile
the two programs on a PC and upload the compiled classes to www.eng.nene.ac.uk.
Highlighted in italics are the changes of which the line

s = (RMIJDBCChatServer)Naming.lookup("//194.81.104.27/RMIJDBCChatServer");

is the most significant as it specifies that the RMI chat server is on


www.eng.nene.ac.uk (194.81.104.27) and that the name of the services (NOT the
server) is RMIJDBCChatServer.

RMIJDBCChatServer.java
This file is the interface file, which is needed by the client for compilation.

import java.rmi.*;

public interface RMIJDBCChatServer extends Remote


{
public String getMessage (int n) throws RemoteException;
public void putMessage (String s) throws RemoteException;
}
RMIJDBCChatServerImpl.java
This is the implementation of the RMI and JDBC based chat server. As with the
earlier RMI chat server, the two main routines, which are used for storing and
retrieving messages are methods getMessage and putMesssage. These two routines
now use JDBC to access the database on 194.81.104.22.

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

public class RMIJDBCChatServerImpl extends UnicastRemoteObject implements


RMIJDBCChatServer
{
public RMIJDBCChatServerImpl () throws RemoteException
{
}

public String getMessage (int n) throws RemoteException


{
String m = "<RMI & JDBC Chat>\n";

try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection con;
con = DriverManager.getConnection
("jdbc:mysql://194.81.104.22/espen", "www", "www");

Statement stmt = con.createStatement ();

ResultSet rs = stmt.executeQuery ("SELECT * from message");

while( rs.next ())


{
m = m + rs.getString (1) + "\n";
}
}
catch (Exception e )
{
System.out.println (e.getMessage ());
e.printStackTrace ();
}
return m;
}

public void putMessage (String s) throws RemoteException


{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection con;
con = DriverManager.getConnection
("jdbc:mysql://194.81.104.22/espen", "www", "www");

Statement stmt = con.createStatement ();


ResultSet rs;

stmt.executeUpdate
("INSERT into message (messg) values ('" + s + "');");
}
catch (Exception e )
{
System.out.println (e.getMessage ());
e.printStackTrace ();
}
}

public static void main (String args []) throws Exception


{
RMIJDBCChatServerImpl t = new RMIJDBCChatServerImpl ();
Naming.rebind ("//localhost/RMIJDBCChatServer", t);
}
}

The changes from the earlier RMI chat server program is shown in italics.

Running the examples.


Before running the examples, you have to make the following changes to the text
shown in bold in the client and server programs.

Client:

In the line:

s = (RMIJDBCChatServer) Naming.lookup ("//194.81.104.27/RMIJDBCChatServer");

Change

//194.81.104.27/RMIJDBCChatServer to //194.81.104.27/bf02abcdRMIJDBCChatServer

Where bf02abcd is your user id.

Server:

In the line:

Naming.rebind ("//localhost/RMIJDBCChatServer", t);

Change

RMIJDBCChatServer to bf02abcdRMIJDBCChatServer

Assuming that the three source files are on www in your home directory (They may be
on a local Windows PC instead) the following is needed to compile and run the
example
sh-2.05$ rmic RMIJDBCChatServerImpl
sh-2.05$ javac RMIJDBCChatServerImpl.java
sh-2.05$ javac RMIJDBCChatClient.java

Note that the server needs to be compiled first as it generated the stub files needed by
the client.

The server needs to be run first. In two windows use the first window to start the
rmiregistry program (There needs only to be one instance of this program running on
the server. You may get an error message if one is already running).

sh-2.05$ rmiregistry

In the second window start the server

sh-2.05$ java RMIJDBCChatServerImpl

The client now needs to connect to the server. To do this the client program needs to
be uploaded to a Windows machine or run from a Linux machine. To run the client,
simply enter at the UNIX or DOS prompt:

java RMIJDBCChatClient

You might also like