Professional Documents
Culture Documents
Test-2 Key
Test-2 Key
Solution:-
Scheme:
Data capturing from Form --- 1
Creating cookies and adding to the response --- 2
<%
String id = request.getParameter("studId");
String name = request.getParameter("studName");
String course = request.getParameter("studCourse");
String fees = request.getParameter("studFees");
Cookie c1 = new Cookie("cid",id);
Cookie c2 = new Cookie("cname",name);
Cookie c3 = new Cookie("ccourse",course);
Cookie c4 = new Cookie("cfees",fees);
response.addCookie(c1);
response.addCookie(c2);
response.addCookie(c3);
response.addCookie(c4);
%>
4. A Bank Customer wants to try Inheritance through Table per class Hierarchy so he created a
base class named Payment, containing attributes like paymentId, amount and two derived
classes namely CreditCard consisting type of CreditCard they used and Cheque containing type
of cheque. So, when the object of derived class is saved, object of base class will also get
stored in the database in a single table. To know the Object of which class is stored we must
use the Discriminator column. So help him to write hibernate mapping xml file for the above
inheritance mapping by applying the Concept of Table per Class Hierarchy.
Solution:-
Scheme:
Proper syntax for tags and attributes -- 1
Spring configuration file -- 2
<hibernate-mapping>
<class name="com.klef.Payment" table="PAYMENT">
<id name="paymentId" column="pid"></id>
<discriminator column="type" length="10"></discriminator>
<property name="amount" column="amount"></property>
Regular_Employee.java:-
Public class Regular_Employee extends Employee{
Private double salary;
Private double bonus;
//setters and getters
}
Contract_Employee.java:
Public class Contract_Employee extends Employee{
Private double pay_per_hour;
Private String contract_duration;
//setters and getters
}
Employee.hbm.xml:-
<hibernate-mapping>
<class name="com.klu.Employee" table="emp">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
TestInsert.java:
-------------------
Class TestInsert{
Public static void main(String args[]){
Configuration c = new Configuration();
c.configure("hibernate.cfg.xml");
SessionFactory factory=
Session session=c.openSession();
Transaction t=session.beginTransaction();
session.save(e1);
session.save(e2);
session.save(e3);
t.commit();
session.close();
System.out.println("success");
6. Develop a Hibernate application to perform all CRUD operations on Product table in the
database.
Solution:-
Sheme:
--> For Mapping file --- 1
--> For Pojo class --- 1
--> For all crud operations ---- 3
Product.java:
public class Product
{
int product_id;
String product_name;
Double product_price;
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public Double getProduct_price() {
return product_price;
}
public void setProduct_price(Double product_price) {
this.product_price = product_price;
}
}
Product.hbm.xml:
<hibernate-mapping>
<class name="Product" table="product">
</class>
</hibernate-mapping>
HibernateCrudOperations.java:
------------------------------------------
Public class HibernateCrudOperations{
Public static void main(String args[]){
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
p.setProduct_id(100);
p.setProduct_name("ABC");
p.setProduct_price(100.00);
session.save(p);
tx.commit();
System.out.println("insert success"); Session.close();
Object o=session.load(Product.class,new Integer(100));
Product s=(Product)o;
s.setProduct_price(200.00); // implicitly update method will be called..
tx.commit();
System.out.println("Object Updated successfully.....!!"); Session.close();
Object o=session.load(Product.class,new Integer(100));
Product s1=(Product)o;
Session.delete(s1);
Tx.commit();
System.out.println("Object deleted"); Session.close();
Object o=session.load(Product.class,new Integer(100));
Product s2=(Product)o;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
dao.insertRecord(e);
*/
System.out.println("Specific Student Details:");
Employee x = dao.loadSpecificEmployee(1004);
System.out.println("Id:" + x.getId());
System.out.println("Name:" + x.getName());
System.out.println("Contact No:" + x.getContactNo());
System.out.println("Amount Paid:" + x.getAmountPaid());
System.out.println("status:" + x.getStatus());
Part-C (2 X 9M=18M)
Answer ALL Questions
Q. No. 9,10 from CO3 with BTL 3
Q. No. 11,12 from CO4 with BTL 3
9. Consider you have a base class(super) named Payment and 2 derived classes(sub
classes) like CreditCard and Check. If you store any sub class object into database, then
automatically it's super class object data also stored in the database. But all the data
will be stored in single table which is super class table. So please develop the Hibernate
application based on the above details.
Solution:
Scheme:
POJO Classes (Base Class – Payment, Derived Classes – CreditCard & Check) –
2M
Mapping file – 3M
Configuration file – 2M
Java Main Class to Save Objects – 2M
Payment.java
public class Payment
{
}
DebitCard.java
public class DebitCard extends Payment
{
</hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class"<oracle.jdbc.driver.OracleDriver</property>
<mapping resource="payment.hbm.xml"/>
</session-factory"
</hibernate-configuration>
Client.java
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Client
{
public static void main(String args[])
{
SessionFactory factory=new Configuration().configure().buildSessionFactory();
Session session=factory.openSession();
Payment payment=new Payment();
payment.setPaymentId(5);
payment.setAmount(30000);
CreditCard creditcard=new CreditCard();
creditcard.setCardNumber(45);
creditcard.setCardType("visa");
DebitCard debitcard=new DebitCard();
debitcard.setPinNumber(6432);
debitcard.setCardName("ANDHRA BANK");
Transaction transaction=session.beginTransaction();
session.save(payment);
session.save(creditcard);
session.save(debitcard);
transaction.commit();
session.close();
System.out.println("Payment details all are inserted using Table Per Subclass");
}
}
(Or)
10. Develop a web application to store Employee details of small scale industry using MVC
design pattern with the following details:
create an employee form with required fields
create a controller jsp to use Model component
create a Model component
create a view component which will generate result to the client.
Solution:
Index.html – 1M
Java Bean (Employee) – 2M
Controller (Servlet) – 3M
View (JSP) – 3M
index.html
<html>
<head>
<title>MVC Demo</title>
</head>
<body>
<form method="post" action="empdetails">
<table align="center" style="height:150px">
<tr>
<td><b>Enter Employee ID</b></td>
<td><input type="number" name="id" placeholder="Enter Emp ID" required></td>
</tr>
<tr>
<td><b>Enter Employee Name</b></td>
<td><input type="text" name="name" placeholder="Enter Emp Name" required></td>
</tr>
<tr>
<td><b>Select Gender</b></td>
<td>Male<input type="radio" name="gender" value="male"
required> Female<input type="radio" name="gender" value="female"
required></td>
</tr>
<tr>
<td align="center"><input type="submit" value="Submit"></td>
<td align="center"><input type="reset" value="Clear"></td>
</tr>
</table>
</form>
</body>
</html>
Employee.java:
Package Demo;
Public class Employee
{
Private int id;
Private String name;
Private String gender;
web.xml
GetEmpDetails.java
Import Demo.Employee
Import javax.servlet.http.*;
Public class GetEmpDetails extends HttpServlet
{
Public void service(HttpServletRequest req,HttpServletResponse res) throws Exception
{
Int id = Integer.parseInt(req.getParameter(“id”));
String name = req.getParameter(“name”);
String gender = req.getParameter(“gender”);
ViewEmpDetails.jsp
<%@page import="Demo.Employee"%>
<%
Employee myempbean = (Employee)
request.getAttribute("empbean");
out.println(myempbean.getId());
out.println("<br>");
out.println(myempbean.getName());
out.println("<br>");
out.println(myempbean.getGender());
%>
11. Gowtham is developing an application for one of the educational organization. He is
using Hibernate framework to retrieve student data of that organization. So please help
him to develop that application to fetch(retrieving) data and supporting xml files.
POJO Class – 2M
Mapping File – 2M
Configuration File – 3M
Display Records – 2M
{
private int id;
private String name;
private String dept;
//getter and setter methods here
}
Employee.hbm.xml (Mapping File)
<hibernate-configuration>
<session-factory>
<property
name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.user">system</property>
<property name="connection.password">admin</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="Student.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
ViewRecord.java
import org.hibernate.*;
import org.hibernate.cfg.*;
(Or)
12. A professor wants to test his student whether he knows about a concept called Spring setter
injection with List Collection by giving following details.
Create a spring Bean which is having a dependency called List type.
Configure the spring bean with list of students in spring configuration file
write the client program to access the spring bean
POJO Class – 2M
Configuration File – 4M
Client Program – 3M
Solution:
-----------
Student.java
import java.util.*;
public class Student
{
private int id;
private String name;
private int age;
private List<String> skills;
public Student(int id,String name,int age,List<String> skills)
{
this.id=id;
this.name=name;
this.age=age;
this.skills=skills;
}
public void display()
{
System.out.println("***Student Details");
System.out.println("ID:"+id);
System.out.println("Name:"+name);
System.out.println("Age:"+age);
System.out.println("***Skills***");
for( String skill : skills)
{
System.out.println(skill);
}
}
}
applicationConext.xml
<beans>
<bean id="studentbean" class="Student">
<constructor-arg value="100"/>
<constructor-arg value="RAM"/>
<constructor-arg value="20"/>
<constructor-arg>
<list>
<value>C</value>
<value>CPP</value>
<value>JAVA</value>
</list>
</constructor-arg>
</bean>
</beans>
ClientController.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class ClientController
{
public static void main(String args[])
{