Sunday, January 12, 2014

RMI-JDBC Java program 2

// RMI  Interface declaration Hello

    import java.rmi.Remote;
    import java.rmi.RemoteException;

    public interface Hello extends Remote {
int insRec(String s0,String s1,String s2) throws RemoteException;
    }

-------------------------------------------------------------------------

//RMI Server connecting with JDBC-ODBC Driver

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.*;
import java.util.*;

public class HelloImpl extends UnicastRemoteObject implements Hello {

    public HelloImpl() throws RemoteException {
super();
    }
int c_row=1;
int n_row =0;
ArrayList al;
    public ArrayList retRec(String sb) {
al=new ArrayList();
  try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection   c =  DriverManager.getConnection("jdbc:odbc:mysql");
         Statement  st = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  ResultSet.CONCUR_UPDATABLE);
         ResultSet r = st.executeQuery("select * from limbs");
         String s =sb;
r.last();
n_row = r.getRow();
System.out.println("No of rows    : "+n_row);
System.out.println("current row    : "+c_row);


if(s.equals("First")){
c_row=1;
r.absolute(c_row);

  System.out.println("Current row is  "+c_row);
String th = r.getString(1);
                int le    = r.getInt(2);
              int ar  = r.getInt(3);
                                 
al.add(th);
al.add(new Integer(le));
al.add(new Integer(ar));
}


if(s.equals("Last")){
c_row = n_row;
r.absolute(c_row);
  System.out.println("Current row is  "+c_row);
String th = r.getString(1);
                int le    = r.getInt(2);
              int ar  = r.getInt(3);

tal.add(th);
al.add(new Integer(le));
al.add(new Integer(ar));
}


if(s.equals("Next")){
if(c_row <n_row){
 c_row = c_row +1;
 r.absolute(c_row);
   System.out.println("Current row is  "+c_row);
 String th = r.getString(1);
                 int le    = r.getInt(2);
               int ar  = r.getInt(3);
 al.add(th);
al.add(new Integer(le));
al.add(new Integer(ar));
}

}
if(s.equals("Previous")){
if(c_row >1){
c_row = c_row -1;
r.absolute(c_row);
System.out.println("Current row is  "+c_row);
String th = r.getString(1);
                int le    = r.getInt(2);
              int ar  = r.getInt(3);
al.add(th);
al.add(new Integer(le));
al.add(new Integer(ar));
}

}


r.close();
st.close();
c.close();

}catch(Exception ee){ee.printStackTrace();}


return al;

}


    }


    public static void main(String args[]) {
try {
   HelloImpl obj = new HelloImpl();

   // Bind this object instance to the name "HelloServer"
   Naming.rebind("HelloServer", obj);

   System.out.println("HelloServer bound in registry");
} catch (Exception e) {
   System.out.println("HelloImpl err: " + e.getMessage());
   e.printStackTrace();
}
   }
}
------------------------------------------------------------------

RMI Client



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
class RmiJdbc implements ActionListener{
JLabel l1,l2,l3;
JButton b1,b2;
JTextField tf1,tf2,tf3;
JFrame f1;
RmiJdbc(){
l1=new JLabel("enter the things");
l2=new JLabel("no of legs");
l3=new JLabel("no of arms");
b1=new JButton("Submit");
b2=new JButton("Cancel");
tf1=new JTextField(15);
tf2=new JTextField(15);
tf3=new JTextField(15);
f1=new JFrame("Gui Demo");
}
public void display(){
b1.addActionListener(this);
b2.addActionListener(this);
f1.setLayout(new FlowLayout());
f1.add(l1);
f1.add(tf1);
f1.add(l2);
f1.add(tf2);
f1.add(l3);
f1.add(tf3);
f1.add(b1);
f1.add(b2);
f1.setSize(100,100);
f1.setVisible(true);
}


public void actionPerformed(ActionEvent e){
//creating rmi server connection
Hello obj=null;
int nrec=0;
try { 
            obj = (Hello)Naming.lookup("HelloServer"); 
           nrec= obj.insRec(tf1.getText(),tf2.getText(),tf3.getText()); 
   System.out.println(nrec);
        } catch (Exception ex) { 
            System.out.println("HelloApplet exception: " + ex.getMessage()); 
            ex.printStackTrace(); 
        } 

}
public static void main(String arg[]){
RmiJdbc gj1=new RmiJdbc();
gj1.display();
}
}

Post a Comment