0% found this document useful (0 votes)
46 views14 pages

Ass 5

The document contains code for a Spring application that manages event data using an MVC framework. It defines Event and ContactDetails POJO classes, XML configuration files, DAO interfaces and implementations to perform CRUD operations on events stored in a database using JDBC templates. The main class wires the beans and controllers to fetch and display event contact details by calling DAO methods.

Uploaded by

Dharan m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views14 pages

Ass 5

The document contains code for a Spring application that manages event data using an MVC framework. It defines Event and ContactDetails POJO classes, XML configuration files, DAO interfaces and implementations to perform CRUD operations on events stored in a database using JDBC templates. The main class wires the beans and controllers to fetch and display event contact details by calling DAO methods.

Uploaded by

Dharan m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Q2

Beans.xml

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<context:annotation-config/>
<bean id="address" class="com.kumaran.address">

<property name="line1" value="Kattoor Road" />


<property name="line2" value="PN Palayam" />
<property name="city" value="Coimbatore" />
<property name="pincode" value="641037" />
</bean>
<bean id="contactdetails" class="com.kumaran.ContactDetails">
<property name="mobileNumber" value="9876543210" />
<property name="alternateMobileNumber" value="1234567890" />
<property name="landlineNumber" value="22536458" />
<property name="email" value="[email protected]" />
</bean>

</beans>

-------------------------------------------------------------------------------------
Main.java

package com.kumaran;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

ContactDetails contactdetails= context.getBean("contactdetails",ContactDetails.class);

contactdetails.display();

ContactDetails.java

package com.kumaran;

import org.springframework.beans.factory.annotation.Autowired;

public class ContactDetails {


String mobileNumber;
String alternateMobileNumber;
String landlineNumber;
String email;
address address;

public ContactDetails() {

public void setMobileNumber(String mobileNumber) {


this.mobileNumber = mobileNumber;
}
public String getMobileNumber() {
return mobileNumber;
}

public void setAlternateMobileNumber(String alternateMobileNumber) {


this.alternateMobileNumber = alternateMobileNumber;
}
public String getAlternateMobileNumber() {
return alternateMobileNumber;
}

public void setLandlineNumber(String landlineNumber) {


this.landlineNumber = landlineNumber;
}
public String getLandlineNumber() {
return landlineNumber;
}

public void setEmail(String email) {


this.email = email;
}
public String getEmail() {
return email;
}

public address getAddress() {


return address;
}
@Autowired
public void setAddress(address address) {
this.address = address;
}

public void display() {

System.out.println("Mobile Number : "+ mobileNumber);


System.out.println("Alternate mobile number" + alternateMobileNumber);
System.out.println("Landline number" + landlineNumber);
System.out.println("Email" + email);
address.display();
}
}
Contact.java

package com.kumaran;

public class address {

String line1;
String line2;
String city;
int pincode;

public String getLine1() {


return line1;
}
public void setLine1(String line1) {
this.line1 = line1;
}
public String getLine2() {
return line2;
}
public void setLine2(String line2) {
this.line2 = line2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
public address() {

}
public address(String line1, String line2, String city, int pincode) {
super();
this.line1 = line1;
this.line2 = line2;
this.city = city;
this.pincode = pincode;
}
public void display() {
System.out.println("Address:"+line1+ "\n" + line2+ "\n" + city+ "\n" +
pincode);
}
}
Output:
Q1:

Event.java

package com.kumaran.id;

import java.sql.Date;

import java.text.SimpleDateFormat;

public class Event {

int id;

String eventName;

String eventOrganiserName;

String onDay;

int eventFare;
public Event() {

super();

// TODO Auto-generated constructor stub

public Event(int id, String eventName, String eventOrganiserName, String onDay, int eventFare)
{

super();

this.id = id;

this.eventName = eventName;

this.eventOrganiserName = eventOrganiserName;

this.onDay = onDay;

this.eventFare = eventFare;

public int getId() {

return id;

public void setId(int id) {

this.id = id;

public String getEventName() {

return eventName;

public void setEventName(String eventName) {

this.eventName = eventName;

}
public String getEventOrganiserName() {

return eventOrganiserName;

public void setEventOrganiserName(String eventOrganiserName) {

this.eventOrganiserName = eventOrganiserName;

public String getOnDay() {

return onDay;

public void setOnDay(String onDay) {

this.onDay = onDay;

public int getEventFare() {

return eventFare;

public void setEventFare(int eventFare) {

this.eventFare = eventFare;

EventController.java

package com.kumaran.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import com.kumaran.dao.EventDao;

import com.kumaran.id.Event;

@Controller

public class EventController {

@Autowired

EventDao dao;//will inject dao from xml file

@RequestMapping("/eventform")

public String showform(Model m){

m.addAttribute("command", new Event());

return "eventform";

@RequestMapping(value="/save",method = RequestMethod.POST)

public String save(@ModelAttribute("event") Event event){

dao.save(event);

return "viewevent";

@RequestMapping("/viewemp")
public String viewemp(Model m){

List<Event> list=dao.getEvents();

m.addAttribute("list",list);

return "viewevent";

@RequestMapping(value="/editemp/{id}")

public String edit(@PathVariable int id, Model m){

Event event=dao.getEventById(id);

m.addAttribute("command",event);

return "eventform";

@RequestMapping(value="/editsave",method = RequestMethod.POST)

public String editsave(@ModelAttribute("event") Event event){

dao.update(event);

return "redirect:/viewevent";

@RequestMapping(value="/deleteemp/{id}",method = RequestMethod.GET)

public String delete(@PathVariable int id){

dao.delete(id);

return "redirect:/viewevent";

EventDaO.jva
package com.kumaran;

import java.util.ArrayList;

import java.util.List;

public interface EventDAO {

public int save(Event p);

public int update(Event p);

public int delete(int id);

public Event getEventById(int id);

public List<Event>getEvents();

Implem

package com.kumaran;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class EventDAOImpl implements EventDAO{


JdbcTemplate template;

public void setTemplate(JdbcTemplate template) {


this.template = template;
}

public int save(Event p){


String sql="insert into eventDetails(eventName,organiserName,fare,date)
values('"+p.getEventName()+"','"+p.getEventOrganiserName()+"',"+p.getEventFare()
+",'"+p.getOnDay()+"')";
return template.update(sql);
}
public int update(Event p){

String sql="update eventDetails set eventName='"+p.getEventName()+"',


organiserName='"+p.getEventOrganiserName()+"',fare="+p.getEventFare()
+",date='"+p.getOnDay()+"' where id="+p.getId()+"";
return template.update(sql);
}
public int delete(int id){
String sql="delete from eventDetails where id="+id+"";
return template.update(sql);
}
public Event getEventById(int id){
String sql="select id,eventName,organiserName,fare,date from eventDetails where
id=?";
return template.queryForObject(sql, new Object[]{id},new
BeanPropertyRowMapper<Event>(Event.class));
}
public List<Event> getEvents(){
String sql="select * from eventDetails";
return template.query(sql,new RowMapper<Event>(){
public Event mapRow(ResultSet rs, int row) throws SQLException {
Event e=new
Event(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(5),rs.getInt(4));

return e;
}
});
}
}

Dharan-servlet

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.kumaran.controllers"></context:component-
scan>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">


<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eventsdetails"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">


<property name="dataSource" ref="ds"></property>
</bean>

<bean id="dao" class="com.kumaran.dao.EventDao">


<property name="template" ref="jt"></property>
</bean>
</beans>

Index.jsp

<a href="eventform">Add Event details</a>


<a href="viewevent">View Event details</a>

Eventeditform.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<h1>Edit Events</h1>
<form:form method="POST" action="/crud/editsave">
<table >
<tr>
<td></td>
<td><form:hidden path="id" /></td>
</tr>
<tr>
<td>Event name : </td>
<td><form:input path="eventName" /></td>
</tr>
<tr>
<td>Event Organiser name:</td>
<td><form:input path="eventOrganiserName" /></td>
</tr>
<tr>
<td>Date :</td>
<td><form:input path="onDay" /></td>
</tr>
<tr>
<td>Event fare : </td>
<td><form:input path="eventFare" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Edit Save" /></td>
</tr>
</table>
</form:form>
Eventform.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<h1>Add New Event</h1>


<form:form method="post" action="save">
<table >
<tr>
<td>Event name : </td>
<td><form:input path="eventName" /></td>
</tr>
<tr>
<td>Event Organiser name :</td>
<td><form:input path="eventOrganiserName" /></td>
</tr>
<tr>
<td>Date :</td>
<td><form:input path="onDay" /></td>
</tr>
<tr>
<td>Event fare :</td>
<td><form:input path="eventFare" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" /></td>
</tr>
</table>
</form:form>
Viewevent.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<h1>Events List</h1>
<table border="2" width="70%" cellpadding="2">
<tr><th>Event name</th><th>EventOrganiser</th><th>Date</th><th>Event fare</tr>
<c:forEach var="event" items="${list}">
<tr>
<td>${event.eventName}</td>
<td>${event.eventOrganiserName}</td>
<td>${event.onDay}</td>
<td>${event.eventFare}</td>
<td><a href="editevent/${event.id}">Edit</a></td>
<td><a href="deleteevent/${event.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
<br/>
<a href="eventform">Add New Event</a>

Output:

You might also like