Csy3020 JDBC Rmi
Csy3020 JDBC Rmi
Csy3020 JDBC Rmi
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.
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
Database changed
mysql> create table message (messg varchar (100));
Query OK, 0 rows affected (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.*;
try
{
s = (RMIJDBCChatServer) Naming.lookup
("//194.81.104.27/RMIJDBCChatServer");
}
catch (NotBoundException n) {}
catch (MalformedURLException m){}
catch (RemoteException m){}
}
try
{
message = txArea.getText ();
s.putMessage (message);
System.out.println ("Message: " + message);
}
catch (RemoteException i) {}
}
}
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");
RMIJDBCChatServer.java
This file is the interface file, which is needed by the client for compilation.
import java.rmi.*;
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection con;
con = DriverManager.getConnection
("jdbc:mysql://194.81.104.22/espen", "www", "www");
stmt.executeUpdate
("INSERT into message (messg) values ('" + s + "');");
}
catch (Exception e )
{
System.out.println (e.getMessage ());
e.printStackTrace ();
}
}
The changes from the earlier RMI chat server program is shown in italics.
Client:
In the line:
Change
//194.81.104.27/RMIJDBCChatServer to //194.81.104.27/bf02abcdRMIJDBCChatServer
Server:
In the line:
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
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