Remote Objects for Database Access
PROBLEM STATEMENT
Write an RMI program in Java to access a database and also must have appropriate methods for inserting,deleting a retrieval of records from the database.
SOURCE CODE
face1.java
faceserver.java
faceclient.java
OUTPUT
Press 1 to view records
Press 2 to add records
Press 3 to delete records
Enter Your Choice:
2
Enter Eno:
113
Enter Employee name:
Vinita
Press any key to continue . . .
Press 1 to view records
Press 2 to add records
Press 3 to delete records
Enter Your Choice:
1
Eno Employee names:
545 Satish
876 Sameer
898 Prashant
546 Sai
989 Farhan
656 Amit
675 Crima
101 Nidhi
113 Vinita
Press any key to continue . . .
PROBLEM STATEMENT
Write an RMI program in Java to access a database and also must have appropriate methods for inserting,deleting a retrieval of records from the database.
SOURCE CODE
face1.java
import java.rmi.*;
import java.rmi.server.*;
interface face1 extends Remote
{
public String[] retrieve() throws RemoteException;
public void addName(int eno,String name) throws RemoteException;
public void del(String eno) throws RemoteException;
}
import java.rmi.server.*;
interface face1 extends Remote
{
public String[] retrieve() throws RemoteException;
public void addName(int eno,String name) throws RemoteException;
public void del(String eno) throws RemoteException;
}
faceserver.java
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;
import sun.jdbc.odbc.*;
public class faceserver extends UnicastRemoteObject implements face1
{
public int count;
public String roll[];
public String name[];
public faceserver() throws RemoteException
{
}
public String[] retrieve() throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c1 = DriverManager.getConnection("jdbc:odbc:mast");
Statement s1 = c1.createStatement();
ResultSet rs = s1.executeQuery("select * from mast");
int count=0;
int i;
name = new String[20];
while(rs.next())
{
name[count] = rs.getString(1) + " " + rs.getString(2);
count++;
}
roll = new String[count];
for(i=0;i<count;i++)
{
roll[i] = name[i];
}
}
catch(Exception e)
{
e.printStackTrace();
}
return roll;
}
public void addName(int eno,String name) throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mast");
String s1 = "insert into mast values(?,?)";
PreparedStatement stmt = con.prepareStatement(s1);
stmt.setInt(1,eno);
stmt.setString(2,name);
stmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void del(String no) throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mast");
String s1 = "delete from mast where EmpNo =no ";
Statement s2 = con.createStatement();
s2.execute("delete from mast where EmpNo =no ");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String ae[]) throws Exception
{
faceserver f1 = new faceserver();
System.out.println("Trying to register the object");
Naming.bind("NAME1",f1);//f1 is given an identity NAME
System.out.println("Object registered");
}
}
import java.rmi.server.*;
import java.sql.*;
import sun.jdbc.odbc.*;
public class faceserver extends UnicastRemoteObject implements face1
{
public int count;
public String roll[];
public String name[];
public faceserver() throws RemoteException
{
}
public String[] retrieve() throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c1 = DriverManager.getConnection("jdbc:odbc:mast");
Statement s1 = c1.createStatement();
ResultSet rs = s1.executeQuery("select * from mast");
int count=0;
int i;
name = new String[20];
while(rs.next())
{
name[count] = rs.getString(1) + " " + rs.getString(2);
count++;
}
roll = new String[count];
for(i=0;i<count;i++)
{
roll[i] = name[i];
}
}
catch(Exception e)
{
e.printStackTrace();
}
return roll;
}
public void addName(int eno,String name) throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mast");
String s1 = "insert into mast values(?,?)";
PreparedStatement stmt = con.prepareStatement(s1);
stmt.setInt(1,eno);
stmt.setString(2,name);
stmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void del(String no) throws RemoteException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mast");
String s1 = "delete from mast where EmpNo =no ";
Statement s2 = con.createStatement();
s2.execute("delete from mast where EmpNo =no ");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String ae[]) throws Exception
{
faceserver f1 = new faceserver();
System.out.println("Trying to register the object");
Naming.bind("NAME1",f1);//f1 is given an identity NAME
System.out.println("Object registered");
}
}
faceclient.java
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public abstract class faceclient
{
public static void main(String ad[]) throws Exception
{
BufferedReader br;
String name,en;
int eno,choice;
face1 f2 =(face1)Naming.lookup("NAME1");
System.out.println("Press 1 to view records");
System.out.println("Press 2 to add records");
System.out.println("Press 3 to delete records");
System.out.println("Enter Your Choice:");
br=new BufferedReader(new InputStreamReader(System.in));
en=br.readLine();
choice=Integer.parseInt(en);
switch(choice)
{
case 1:
{
String a[] = f2.retrieve();
System.out.println("Eno Employee names:");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
break;
}
Case 2:
{
System.out.println("Enter Eno:");
br=new BufferedReader(new InputStreamReader(System.in));
en=br.readLine();
eno=Integer.parseInt(en);
System.out.println("Enter Employee name:");
br=new BufferedReader(new InputStreamReader(System.in));0
name=br.readLine();
f2.addName(eno,name);
break;
}
case 3:
{
System.out.println("Enter Eno to delete:");
br=new BufferedReader(new InputStreamReader(System.in));
en=br.readLine();
f2.del(en);
}
default:
{
System.exit(0);
}
}
}
}
import java.rmi.server.*;
import java.io.*;
public abstract class faceclient
{
public static void main(String ad[]) throws Exception
{
BufferedReader br;
String name,en;
int eno,choice;
face1 f2 =(face1)Naming.lookup("NAME1");
System.out.println("Press 1 to view records");
System.out.println("Press 2 to add records");
System.out.println("Press 3 to delete records");
System.out.println("Enter Your Choice:");
br=new BufferedReader(new InputStreamReader(System.in));
en=br.readLine();
choice=Integer.parseInt(en);
switch(choice)
{
case 1:
{
String a[] = f2.retrieve();
System.out.println("Eno Employee names:");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
break;
}
Case 2:
{
System.out.println("Enter Eno:");
br=new BufferedReader(new InputStreamReader(System.in));
en=br.readLine();
eno=Integer.parseInt(en);
System.out.println("Enter Employee name:");
br=new BufferedReader(new InputStreamReader(System.in));0
name=br.readLine();
f2.addName(eno,name);
break;
}
case 3:
{
System.out.println("Enter Eno to delete:");
br=new BufferedReader(new InputStreamReader(System.in));
en=br.readLine();
f2.del(en);
}
default:
{
System.exit(0);
}
}
}
}
OUTPUT
Press 1 to view records
Press 2 to add records
Press 3 to delete records
Enter Your Choice:
2
Enter Eno:
113
Enter Employee name:
Vinita
Press any key to continue . . .
Press 1 to view records
Press 2 to add records
Press 3 to delete records
Enter Your Choice:
1
Eno Employee names:
545 Satish
876 Sameer
898 Prashant
546 Sai
989 Farhan
656 Amit
675 Crima
101 Nidhi
113 Vinita
Press any key to continue . . .
0 comments:
Confused? Feel free to ask
Post a Comment