I have made an
applet and I want to call my rmi Server. My server is register and the object is bounded to the registry. However, I took the applet and the stub and put in within another computer. Nothing happens, How do I get it to work. I think that the stub is not seen in my client application, but I don't know what to do. Could anyone please help?
This is my html (main.html):
<code>
<HTML>
<HEAD><TITLE>Data Base Demo</TITLE></HEAD>
<BODY>
<CENTER>
<H1>DataBase using RMI</H1>
<APPLET CODE="DBApplet.class" WIDTH = 600 HEIGHT = 600>
</APPLET>
</CENTER>
</BODY>
</HTML>
</code>
This is my applet (DBApplet.class):
<code>
import java.rmi.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class DBApplet extends Applet {
private Button submit = new Button("Submit Query");
private TextField sql = new TextField("",50);
private TextArea ans = new TextArea();
private DbServer obj;
public void init() {
try {
obj = (DbServer)Naming.lookup("/Server");
this.add(sql);
this.add(submit);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try {
String s = sql.getText();
ans.append(obj.query(s));
} catch(Exception e1) {
System.out.println("DBApplet exception :"+e1.getMessage());
e1.printStackTrace();
}
}
});
this.add(ans);
} catch (Exception e) {
System.out.println("DBApplet exception :" + e.getMessage());
e.printStackTrace();
}
}
}
</code>
This is my Server:
<code>
public interface DbServer extends java.rmi.Remote {
public abstract String query(String sql) throws java.rmi.RemoteException;
}
import java.sql.*;
import java.rmi.*;
import java.rmi.server.*;
public class Server extends UnicastRemoteObject implements DbServer {
private Connection connection;
private Statement statement;
private ResultSet rs;
private String name;
public Server(String Sname, String Spassword) throws RemoteException {
String name = Sname;
String password = Spassword;
String connectionURL = "jdbc:mysql://192.168.0.154:3306/login";
connection = null;
statement = null;
rs = null;
try {
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
connection = DriverManager.getConnection(connectionURL, name, password);
} catch(Exception e) {
System.out.println("Failed " + e.getMessage());
}
}
public void display() {
try {
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM log");
while (rs.next()) {
System.out.println(rs.getString(1) + " - " + rs.getString(2) + " - " + rs.getString(3));
}
}
catch(SQLException e) {
System.out.println(">>Failed " + e.getMessage());
}
}
public String query(String sql) {
try {
Statement st = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet r = st.executeQuery(sql);
StringBuffer s = new StringBuffer();
while (r.next()) {
s.append(r.getString(1) + " - " + r.getString(2) + " - " + r.getString(3) + "\n");
}
return s.toString() ;
} catch(SQLException e) {
System.out.println(">>Failed " + e.getMessage());
}
return "";
}
public static void main(String [] args) throws RemoteException {
// Starting RMIRegistry programmatically
try {
java.rmi.registry.LocateRegistry.createRegistry(1099);
System.out.println("RMI registry ready.");
} catch (Exception e) {
System.out.println("Exception starting RMI registry:");
e.printStackTrace();
}
//Security Policy
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
//JNDI Path, naming which is going to belooked up from the server
String name = "/Server";
//Initiating Server and binding the object to the Server
try {
Server obj = new Server("rassaad", "newtrade");
Naming.rebind(name, obj);
System.out.println("Server bound in registry");
} catch (Exception e) {
System.out.println(">> Impl err: " + e.getMessage());
e.printStackTrace();
}
}
}
</code>
[ February 28, 2002: Message edited by: bobby, morkos ]