Sunday, January 12, 2014

RMI-JDBC Java program 1

// RMI interface declaration Hello

    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.util.*;

    public interface Hello extends Remote {
ArrayList retRec(String s) throws RemoteException;
    }
---------------------------------------------------

//RMI Server


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);

al.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 GUI client


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
class GuiJdbc1 implements ActionListener{

JLabel l1,l2,l3;
JButton b1,b2,b3,b4;
JTextField tf1,tf2,tf3;
JFrame f1;
int c_row=1;
int n_row =0;
Hello obj=null;
GuiJdbc1(){
l1=new JLabel("enter the things");
l2=new JLabel("no of legs");
l3=new JLabel("no of arms");

b1=new JButton("First");
b2=new JButton("Last");
b3=new JButton("Next");
b4=new JButton("Previous");
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);
b3.addActionListener(this);
b4.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.add(b3);
f1.add(b4);

f1.setSize(300,300);
f1.setVisible(true);
}


public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
ArrayList al;
try {
            obj = (Hello)Naming.lookup("HelloServer");
            al = obj.retRec(s);
tf1.setText(""+(String)al.get(0));
tf2.setText(""+(Integer)al.get(1));
tf3.setText(""+(Integer)al.get(2));
        } catch (Exception ex) {
            System.out.println("HelloApplet exception: " + ex.getMessage());
            ex.printStackTrace();
        }



}
public static void main(String arg[]){
GuiJdbc1 gj2=new GuiJdbc1();
gj2.display();
}
}
----------------------------------------------------------------



No comments: