Ajt PRS
Ajt PRS
Practical-9
Aim: Create database of student subject-wise data and retrieve all data using JSP and generate xml
structure along with DTD and XML Schema definition Write down a program which demonstrates the
core tag of JSTL
OUTPUT : -
FORM FOR INSERTING DATA : - BASIC OUTPUT ON THE WEB : - XML – STYLED O/P : -
1
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
Practical-10
Aim: Implement a chat application using TCP. Write an RMI application where client supplies two numbers &
server response by addition. Provide your custom security policy for this application.
import java.io.*; import java.net.*; public class ChatServer { public static void main(String[] args) { try { ServerSocket server = new
ServerSocket(1234); System.out.println("Server started..."); while(true) { Socket clientSocket = server.accept(); new
ClientHandler(clientSocket).start(); } } catch(Exception e) { System.out.println(e); } } static class ClientHandler extends Thread { Socket
clientSocket; ClientHandler(Socket socket) { this.clientSocket = socket; } public void run() { try { BufferedReader in = new
BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true); String message; while((message = in.readLine()) != null) { System.out.println("Client: " +
message); out.println("Server: " + message); } } catch(Exception e) { System.out.println(e); } finally { try { clientSocket.close(); }
catch(IOException e) { System.out.println(e); } } } } }
import java.io.*;import java.net.*;public class ChatClient {public static void main(String[] args) {try { Socket socket=new Socket("localhost",
1234); BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new
PrintWriter(socket.getOutputStream(), true); BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));String
message; while(true) {System.out.print("Enter message: "); message =
userInput.readLine();out.println(message);System.out.println(in.readLine()); }} catch(Exception e){System.out.println(e);}}}}
1. Compile the Code: Save the server code in a file named ChatServer.java. Save the client code in a file named ChatClient.java.
Compile both the server and client code using the following commands:
javac ChatServer.java
javac ChatClient.java
java ChatClient - You can run multiple clients as needed to simulate chat communication.
4. Chat Communication: The client can send messages, and the server will echo the same message back.
OUTPUT : -
Basic Steps to implement RMI application where client supplies two numbers & server responds by addition.
Provide the custom security policy for this application.
import java.rmi.*; public interface AddNumbers extends Remote { public int add(int num1, int num2) throws RemoteException; }
import java.rmi.*; import java.rmi.server.*; public class AddNumbersImpl extends UnicastRemoteObject implements AddNumbers { public
AddNumbersImpl() throws RemoteException { super(); } public int add(int num1, int num2) throws RemoteException { return num1 +
num2; } public static void main(String[] args) { try { AddNumbersImpl obj = new AddNumbersImpl();
1
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
import java.rmi.*; public class AddNumbersClient { public static void main(String[] args) { try { AddNumbers obj = (AddNumbers)
Naming.lookup("rmi://localhost:1099/AddNumbers"); int result = obj.add(10, 20); System.out.println("Result: " + result); } catch(Exception
e) { System.out.println("Client Error: " + e); } } }
5. Steps to Run:
1. Open CMD
2. Make the path to the relevant folder where all files are located
javac *.java
start rmiregistry
5. Run Server:
java AddNumbersImpl
7. Run Client:
java AddNumbersClient
8. (Optional) Security Policy file: Make sure the security.policy file is used when running with a policy:
2
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
Practical-11
Aim: Implement simple framework using hibernates and studies its architecture
Basic Steps to Implement a Simple Hibernate Framework and Study Its Architecture
package com; import jakarta.persistence.Entity; import jakarta.persistence.Id; @Entity public class Student { @Id
private int id; private String name; private String course; // Constructors public Student() {} public Student(int id, String
name, String course) { this.id = id; this.name = name; this.course = course; } // Getters and Setters
public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; }
public void setName(String name) { this.name = name; } public String getCourse() { return course; }
public void setCourse(String course) { this.course = course; } }
1
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
package com; import org.hibernate.Session; import org.hibernate.Transaction; public class MainApp { public static void
main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction =
session.beginTransaction(); Student student = new Student(6, "HibernateUser", "ORM with Hibernate");
session.save(student); transaction.commit(); session.close(); System.out.println("Student record inserted successfully using
Hibernate framework."); }}
OUTPUT : -
Architecture Summary:
2
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
Practical-12
Aim: Use Hibernate Query Language to insert, update and delete records in the database.
Steps for HQL-based CRUD Operations in Hibernate:
1. Configure Hibernate: Create a configuration file (hibernate.cfg.xml) to set up database connection and
mapping.
2. Create Entity Class: Use Java classes with annotations like @Entity, @Id, @Column to represent database
tables.
6. Insert Record: Use HQL "INSERT INTO" query or session.save() to insert a new record.
7. Update Record: Use HQL "UPDATE" query with session.createQuery() and executeUpdate().
8. Delete Record: Use HQL "DELETE" query with conditions to remove records.
10. Close Session: Always close session and SessionFactory.CREATE DATABASE STUDENTDB
To Run Steps for CRUD Operations in Java using Hibernate (Apache Derby)
2. Create Apache Derby DB: No explicit DB creation in Derby client mode, it’s auto-created on connect (StudentDB).
package com; import javax.persistence.*; @Entity @Table(name="STUDENT") public class Student { @Id private int id; private
String name; private int age; private String course; public Student() {} public Student(int id, String name, int age, String course)
{ this.id = id; this.name = name; this.age = age; this.course = course; } public int getId() { return id; } public void setId(int id) {
this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int
getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCourse() { return course; } public void
setCourse(String course) { this.course = course; } }
HibernateUtil.java
package com; import org.hibernate.*; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final
SessionFactory factory; static { try { factory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) {
throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return factory; } }
1
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
package com; import org.hibernate.*; import java.util.Scanner; public class InsertStudent { public static void main(String[]
args) { Scanner sc = new Scanner(System.in); System.out.print("Enter ID: "); int id = sc.nextInt(); sc.nextLine();
System.out.print("Enter Name: "); String name = sc.nextLine(); System.out.print("Enter Age: "); int age = sc.nextInt();
sc.nextLine(); System.out.print("Enter Course: "); String course = sc.nextLine(); Session session =
HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Student s = new Student(id,
name, age, course); session.save(s); tx.commit(); session.close(); System.out.println("✔ Student Inserted!"); } }
package com; import org.hibernate.*; import java.util.Scanner; public class UpdateStudent { public static void main(String[]
args) { Scanner sc = new Scanner(System.in); System.out.print("Enter ID to Update: "); int id = sc.nextInt(); sc.nextLine();
System.out.print("New Name: "); String name = sc.nextLine(); System.out.print("New Age: "); int age = sc.nextInt();
sc.nextLine(); System.out.print("New Course: "); String course = sc.nextLine(); Session session =
HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); String hql = "UPDATE Student
SET name = :name, age = :age, course = :course WHERE id = :id"; Query query = session.createQuery(hql);
query.setParameter("name", name); query.setParameter("age", age); query.setParameter("course", course);
query.setParameter("id", id); int result = query.executeUpdate(); tx.commit(); session.close(); System.out.println(result > 0 ?
"✔ Updated Successfully!" : "+ Record not found!"); } }
package com; import org.hibernate.*; import java.util.Scanner; public class DeleteStudent { public static void main(String[]
args) { Scanner sc = new Scanner(System.in); System.out.print("Enter ID to Delete: "); int id = sc.nextInt(); Session session =
HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); String hql = "DELETE FROM
Student WHERE id = :id"; Query query = session.createQuery(hql); query.setParameter("id", id); int result =
query.executeUpdate(); tx.commit(); session.close(); System.out.println(result > 0 ? "✔ Deleted Successfully!" : "+ Record
not found!"); } }
OUTPUT SCREENSHOTS : -
Insert data
Update data
Delete data
2
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
Practical-13
Aim: Demonstrate CRUD operation using DAO and Spring Framework API.
Basic Steps to Spring-based Java application which performs CRUD operations on a STUDENT table
using DAO pattern and Apache Derby database.
Table Structure (Apache Derby):
CREATE TABLE STUDENT (ID INT PRIMARY KEY, NAME VARCHAR(255), AGE INT, COURSE VARCHAR(255));
package com.model; public class Student { private int id; private String name; private int age; private String course; public
Student() {} public Student(int id, String name, int age, String course) { this.id = id; this.name = name; this.age = age;
this.course = course; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return
name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int
age) { this.age = age; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course;
}}
package com.dao; import com.model.Student; import java.util.*; public interface StudentDAO { public int insert(Student s);
public int update(Student s); public int delete(int id); public List<Student> getAll(); }
1
IU2241230188 ADVANCED JAVA(AJT) 6CSE-E
dao.update(new Student(1, "Jigar Sindhi", 21, "AI/ML")); dao.delete(1); List<Student> list = dao.getAll(); for (Student s : list)
System.out.println(s.getId() + " " + s.getName() + " " + s.getAge() + " " + s.getCourse()); } }
OUTPUT : -
INSERT DATA
UPDATE DATA
DELETE DATA