This RMI Program to report The content in remote Server.This Code will use to for report your Server.Remote method invocation is best one in Java.In below example code this technique will use.please usea as following steps to run this program.
Steps:
1) create serializale Interface DataBaseAccessMethods
2) create DataBaseAction
3) create ReportServer(Rmi Server)
4) create ReportUser(Client)
5) run commandpromt rmic DataBaseAction
6) run rmiregistry with port
7) run RmiServer(ReportServer)
8) run Rmiclient(ReportUser)
DataBaseAccessMethods.java
import java.rmi.*;
import java.awt.*;
public interface DataBaseAccessMethods extends Remote
{
public String login(String username,String Password)throws RemoteException,Exception;
public void reportMsgs(String user,String msg,String place,String cdate)throws RemoteException,Exception;
public void connection() throws RemoteException,Exception;
public void close() throws RemoteException,Exception;
}
DataBaseAccessMethods.java
In this program Overwrite the DataBaseAccessMethods class...
import java.io.*;
import java.rmi.*;
import java.awt.*;
import java.rmi.server.UnicastRemoteObject;
import java.sql.*;
public class DataBaseAction extends UnicastRemoteObject implements DataBaseAccessMethods
{
String url = "jdbc:odbc:report";
String userID = "";
String password = "";
Statement DataRequest;
Connection Db;
ResultSet Results;
PreparedStatement DataRequest1;
String FirstName;
String LastName;
String printrow;
public void connection() throws RemoteException,Exception
{
//try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
Db = DriverManager.getConnection(url,userID,password);
}
//catch(Exception e)
{
// System.out.println("Connection Error:"+e);
}
}
public DataBaseAction()throws RemoteException
{
//System.out.println("Rmi Server is ready");
super();
}
public String login(String username,String password)throws RemoteException,Exception
{
String name="";
//try
{
connection();
String query = "SELECT * FROM users where Username='"+username+"' and Password='"+password+"'";
DataRequest = Db.createStatement();
Results = DataRequest.executeQuery (query);
boolean Records = Results.next();
if (!Records )
{
name="";
}
else
{
name = Results.getString ("Name") ;
}
close();
}
//catch(Exception e)
{
// System.out.println("Select Action Error:"+e);
}
return name;
}
public void reportMsgs(String user,String msg,String place,String cdate)throws RemoteException,Exception
{
// try
{
connection();
String query = "insert into msgs (users,place,message,Cdate) values ('"+user+"','"+msg+"','"+place+"',#"+cdate+"#)";
System.out.println(query);
DataRequest = Db.createStatement();
DataRequest.executeUpdate(query);
close();
}
// catch(Exception e)
{
// System.out.println("Insert Action Error:"+e);
}
}
public void close() throws RemoteException,Exception
{
//try
{
DataRequest.close();
DataRequest.close();
Db.close();
}
//catch(Exception e)
{
// System.out.println("Close Error:"+e);
}
}
}
Design a RMI server...
DataBaseAccessMethods.java
import java.io.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class ReportServer
{
public static void main(String arg[])throws Exception
{
//try
{
DataBaseAction s1=new DataBaseAction();
Naming.rebind("Reporting",s1);
System.out.println("Report Server is ready");
}
//catch(RemoteException e)
{
// System.out.println(e);
}
}
}
Design a RMI Client....
ReportUserWindow.java
import java.net.*;
import java.io.DataInputStream;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.*;
import java.awt.event.*;
import java.util.Date;
import java.awt.*;
public class ReportUserWindow
{
public static void main(String arg[])throws Exception
{
WindowD win=new WindowD();
}
}
class WindowD extends JFrame implements ActionListener
{
Date d;
JPanel left, right, top, bottom, center;// Panels
JLabel banner;
//JFrame win;
JLabel uname,pass;
JTextField tuname;
JPasswordField tpass;
JButton login,send;
JLabel username,date,msgs,place;
JTextField tusername,tdate,tplace;
JTextArea tmsgs;
JScrollPane smsgs;
WindowD()
{
d=new Date();
setLayout(new BorderLayout());
left = new JPanel();
right = new JPanel();
top = new JPanel();
bottom = new JPanel();
center = new JPanel();
add(left, BorderLayout.WEST);
add(right, BorderLayout.EAST);
add(top, BorderLayout.NORTH);
add(bottom, BorderLayout.SOUTH);
add(center, BorderLayout.CENTER);
uname=new JLabel("Username:");
pass=new JLabel("Password:");
tuname=new JTextField();
tpass=new JPasswordField();
login=new JButton("Login");
center.setLayout(null);
uname.setBounds(20,20,80,20);
center.add(uname);
tuname.setBounds(120,20,80,20);
center.add(tuname);
pass.setBounds(20,50,80,20);
center.add(pass);
tpass.setBounds(120,50,80,20);
center.add(tpass);
login.setBounds(120,80,80,20);
center.add(login);
center.setSize(100,100);
banner = new JLabel("------------Reporter---------");
top.add(banner);
setSize(300, 400);
setVisible(true);
login.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
JButton mi = (JButton)e.getSource();
String s = mi.getText();
if (s.equals("Login"))
{
try
{
String stuname=tuname.getText();
String stpass=tpass.getText();
Object obj=Naming.lookup("Reporting");
DataBaseAccessMethods data=(DataBaseAccessMethods)obj;
String rname=data.login(stuname,stpass);
if(!rname.equals(""))
{
System.out.println("gggg"+rname);
center.removeAll();
repaint();
username=new JLabel("Username:");
date=new JLabel("Date:");
msgs=new JLabel("Messages:");
place=new JLabel("Place:");;
tusername=new JTextField();
tusername.setText(rname);
int date1=d.getDate();
int month=d.getMonth();
int year1=d.getYear();
String sss=d.toGMTString();
tdate=new JTextField();
tdate.setText(""+date1+"/"+month+"/"+year1);
System.out.println(sss);
tmsgs=new JTextArea();
smsgs=new JScrollPane(tmsgs);
tmsgs.setText("");
tplace=new JTextField();
send=new JButton("Send");
send.addActionListener(this);
username.setBounds(20,20,80,20);
tusername.setBounds(120,20,80,20);
date.setBounds(20,50,80,20);
tdate.setBounds(120,50,80,20);
msgs.setBounds(20,80,80,20);
smsgs.setBounds(120,80,80,60);
place.setBounds(20,150,80,20);
tplace.setBounds(120,150,80,20);
send.setBounds(120,180,80,20);
center.add(username);
center.add(tusername);
center.add(date);
center.add(tdate);
center.add(msgs);
center.add(smsgs);
center.add(place);
center.add(tplace);
center.add(send);
}
}
catch(Exception ee)
{
System.out.println(ee);
}
}
if (s.equals("Send"))
{
try
{
String stuname=tusername.getText();
String cdate=tdate.getText();
String msg=tmsgs.getText();
String place=tplace.getText();
Object obj=Naming.lookup("Reporting");
DataBaseAccessMethods data=(DataBaseAccessMethods)obj;
data.reportMsgs(stuname,msg,place,cdate);
tmsgs.setText("");
tplace.setText("");
}
catch(Exception ee)
{
System.out.println(ee);
}
}
}
}
0 comments:
Post a Comment