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
15 January 2020 at 20:54 delete

it is not executed....why??

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

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery



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

I really liked your blog post.Much thanks again. Awesome.
java training
java online training

Reply
avatar

Ads