RMI + JDBC

Practical : 6
Subject : Advanced Java
Aim : Implement Student information system using JDBC and RMI JDBC/Servlet



StudentI.java Code:
--------------------------------------------------------------------------------
import java.rmi.Remote;
import java.sql.*;
import java.util.*;
public interface StudentI extends Remote
{
public abstract ArrayList insert(int id,String name,String branch,int atd) throws Exception;
}
--------------------------------------------------------------------------------
StudentC.java Code:
--------------------------------------------------------------------------------
import java.sql.*;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class StudentC extends UnicastRemoteObject implements StudentI
{
public StudentC() throws Exception
{
super();
}
public ArrayList insert(int id,String name,String branch,int atd) throws Exception
{
ArrayList ar=new ArrayList();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","birju");
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into student values("+id+",'"+name+"','"+branch+"',"+atd+")");
ResultSet rs=stmt.executeQuery("select * from student where id="+id);
rs.next();
int Id=rs.getInt(1);
name=rs.getString(2);
branch=rs.getString(3);
atd=rs.getInt(4);

ar.add(new Integer(Id));
ar.add(name);
ar.add(branch);
ar.add(new Integer(atd));
con.close();
return ar;
}
}
--------------------------------------------------------------------------------
Server.java Code:

--------------------------------------------------------------------------------
import java.rmi.*;
public class Server
{
public static void main(String args[]) throws Exception
{
StudentI obj=new StudentC();
Naming.rebind("stinfo",obj);
System.out.println("Server Started");
}
}
--------------------------------------------------------------------------------
Client.java Code:


--------------------------------------------------------------------------------
import java.rmi.*;
import java.sql.*;
import java.util.*;
public class Client
{
public static void main(String args[]) throws Exception
{
Scanner scan= new Scanner(System.in);
System.out.println("Enter id,name,branch,attendance for student:");
int id=scan.nextInt();
String name=scan.next();
String branch=scan.next();
int atd=scan.nextInt();
StudentI obj=(StudentI)Naming.lookup("stinfo");
ArrayList ar=obj.insert(id,name,branch,atd);
Iterator it=ar.iterator();
System.out.println("Id\tName\tBranch\tAttendance");
System.out.println(it.next()+"\t"+it.next()+"\t"+it.next()+"\t"+it.next()+"\t");
}
}
--------------------------------------------------------------------------------

Output:
Commands

Server

Client

Previous
Next Post »

5 comments

Write comments
priya
AUTHOR
28 January 2019 at 22:45 delete

No more live link in this comments field

Reply
avatar
15 January 2020 at 20:54 delete

it is not executed....why??

Reply
avatar
Anu
AUTHOR
24 May 2020 at 08:07 delete

No more live link in this comments field

Reply
avatar
Revathi
AUTHOR
17 July 2020 at 03:55 delete

No more live link in this comments field

Reply
avatar
Unknown
AUTHOR
10 January 2022 at 04:30 delete

No more live link in this comments field

Reply
avatar

ConversionConversion EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng

Ads