Chapter 4: Remote Object Communication
Program 4.1: Retrieve the Student Score from the database. (Use Concept of JDBC and RMI)
File Name: StudentScoreImpl.java
import java.rmi.*;
public interface StudentScoreInterface extends Remote
{
public double findScore(String name)throws RemoteException;
}
File Name:StudentScoreImpl.java
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.sql.*;
public class StudentScoreImpl extends UnicastRemoteObject implements StudentScoreInterface
{
private PreparedStatement pst;
public StudentScoreImpl()throws RemoteException
{
super(); initilizeDB();
}
protected void initilizeDB()
{
try
{
Class.forName(“com.mysql.jdbc.Driver”); Connection
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/symca”, “root”, “”);
pst = con.prepareStatement(“Select * from scores where
Name=?”);
}
catch(Exception e)
{
System.out.println(e);
}
- }
- public double findScore(String name)throws RemoteException
- {
- double score=-1; try
- {
- pst.setString(1, name);
- ResultSet rs=pst.executeQuery(); if(rs.next())
- {
- score=rs.getDouble(“Score”);
- }
- }
- catch(Exception e)
- {
- }
- System.out.println(score); return score;
- }
}
File Name: StudentScoreRegister.java
import java.rmi.*;
import java.rmi.registry.*;
public class StudentScoreRegister
{
- public static void main(String[] agrs)
- {
- try
- {
- StudentScoreImpl obj = new StudentScoreImpl(); Registry reg = LocateRegistry.getRegistry(); Naming.rebind(“StudentScoreImpl”,obj);
- }
- catch(Exception e)
- {
- }
- }
}
File Name: StudentScoreClient.java
import java.rmi.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentScoreClient extends JApplet
{
- private StudentScoreInterface student;
- private JButton getScore = new JButton(“GetSocre”); private JTextField name = new JTextField();
- private JTextField tfscore = new JTextField(); private JLabel lblname = new JLabel(“Name”); private JLabel lblscore = new JLabel(“Score”); public void init()
- {
- {
student = (StudentScoreInterface)Naming.lookup(“StudentScoreImpl”);
- }
- catch(Exception e)
- {
- }
- JPanel panel = new JPanel(); panel.setLayout(null);
- lblname.setBounds(20, 20, 80, 34); panel.add(lblname); name.setBounds(100, 20, 160, 34); panel.add(name);
- lblscore.setBounds(20, 70, 80, 34); panel.add(lblscore); tfscore.setBounds(100, 70, 160, 34); panel.add(tfscore);
- getScore.setBounds(100, 140, 160, 34); panel.add(getScore, BorderLayout.CENTER); add(panel,BorderLayout.CENTER); getScore.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent ae)
- {
- }
- }
- );
- }
- public void getScore()
- {
- double score = student.findScore(name.getText()); if(score<0)
- tfscore.setText(“Not Found”);
-
- else tfscore.setText(“”+score);
- }
- catch(Exception ex)
- {
- }
- }
- public static void main(String[] args)
- {
- StudentScoreClient applet = new StudentScoreClient(); JFrame frame = new JFrame(); frame.setTitle(“studentscore”); frame.add(applet,BorderLayout.CENTER); frame.setSize(300,240);
- applet.init(); frame.setVisible(true);
- }
- }
Program 4.2: Using MySQL create Library database. Create table Book
(Book_id, Book_name, Book_author) and retrieve the Book information from Library database using Remote Object Communication concept.
File Name:LibraryInterface.java
import java.rmi.*;
public interface LibraryInterface extends Remote
{
public String[] getData()throws RemoteException;
}
LibraryDB.java
import java.rmi.*; import java.rmi.server.*; import java.sql.*;
public class LibraryDB extends UnicastRemoteObject implements LibraryInterface
{
int count=0;
String str[];
public LibraryDB()throws RemoteException
{
super();
}
public String[] getData()
{
try
{
Class.forName(“com.mysql.cj.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/symca”, “root”, “”); Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“select * from library”);
while(rs.next())
{
count++;
}
str=new String[count];
ResultSet rs1=st.executeQuery(“select * from library”); for(int i=0; i<count; i++)
{
rs1.next(); str[i]=rs1.getString(1);
}
}
catch(Exception e)
{
System.out.println(e);
}
return str;
}
}
File Name:LibraryDBClient.java
import java.rmi.*;
public class LibraryDBClient
{
- public LibraryDBClient()
- {
- try
- {
- LibraryInterface library = (LibraryInterface)Naming.lookup(“LibraryDB”); String[] result=library.getData();
- for (String res: result)
- {
- System.out.println(“
- ----- “+”\n“+”|
- “+res+”\n“+” “);
- }
}
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- public static void main(String[] args)
- {
- new LibraryDBClient();
- }
}
File Name:LibraryDBRegister.java
import java.rmi.*;
import java.rmi.registry.*; public class LibraryDBRegister
{
- public static void main(String[] agrs)
- {
- LibraryDB obj = new LibraryDB();
- Registry reg = LocateRegistry.getRegistry(); Naming.rebind(“LibraryDB”,obj);
- }
- catch(Exception e)
- }
}
OUTPUT:
Exercise 1: Using MySQL create Elecrtic_Bill database. Create table Bill (consumer_name, bill_due_date, bill_amount) and retrieve the Bill information from the Elecrtic_Bill database using Remote Object Communication concept.