Module 5 Assignment Java (MCA)
Module 5 Assignment Java (MCA)
1. Write a program to insert, update and delete records from the given table.
package com.jdbc;
public class Employee {
private int id;
private String name;
private float salary;
public int getId() {
return id;
}
public Employee(){}
public Employee(int id, String name, float salary) {
super();
this.id = id;
this.name = name;
this.salary = salary; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public float getSalary() { return salary; }
public void setSalary(float salary) { this.salary = salary; }
}
package com.jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
public class EmployeeDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int saveEmployee(Employee e){
String query="insert into employee values('"+e.getId()+"','"+e.getName()+"','"+e.getSalary()
+"')";
return jdbcTemplate.update(query);
}
public int updateEmployee(Employee e){
String query="update employee set name='"+e.getName()+"',salary='"+e.getSalary()+"' where
id='"+e.getId()+"' ";
return jdbcTemplate.update(query);
}
public int deleteEmployee(Employee e){
String query="delete from employee where id='"+e.getId()+"' ";
return jdbcTemplate.update(query);
} }
config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="e123" class="com.jdbc.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
App.java (main method)
package com.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context= new ClassPathXmlApplicationContext("config.xml");
EmployeeDao dao=(EmployeeDao)context.getBean("e123");
int status= dao.saveEmployee(new Employee(105,"Shweta",10000));
System.out.println(status);
Employee e=new Employee();
e.setId(109);
e.setName("Sandeep");
e.setSalary(200000);
int result= dao.saveEmployee(e);
System.out.println(result);
//Deletion
e.setId(546);
System.out.println(" delete operation"+dao.deleteEmployee(e));
}
}
2. Write a program to demonstrate PreparedStatement in Spring JdbcTemplate
Main App
//PreparedStatement Callback Interface
System.out.println(dao.saveEmployeebyPrepared(new
Employee(108,"Amit",35000)));
3. Write a program in Spring JDBC to demonstrate ResultSetExtractor Interface
System.out.println();
System.out.println("----------------------------------------");