Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainApp {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
- StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
- System.out.println("------- Records Creation ------");
- studentJDBCTemplate.create("Zara", 11);
- studentJDBCTemplate.create("Nuha", 2);
- studentJDBCTemplate.create("Ayan", 15);
- System.out.println("------- Listing Multiple Records -------");
- List<Student> students = studentJDBCTemplate.listStudents();
- for (Student record : students) {
- System.out.print("ID : " + record.getId());
- System.out.print(", Name : " + record.getName());
- System.out.println(", Age : " + record.getAge());
- }
- System.out.println("------ Updating record with ID = ------");
- studentJDBCTemplate.update(2, 20);
- }
- }
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainApp {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
- StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
- System.out.println("------- Records Creation ------");
- studentJDBCTemplate.create("Zara", 11);
- studentJDBCTemplate.create("Nuha", 2);
- studentJDBCTemplate.create("Ayan", 15);
- System.out.println("------- Listing Multiple Records -------");
- List<Student> students = studentJDBCTemplate.listStudents();
- for (Student record : students) {
- System.out.print("ID : " + record.getId());
- System.out.print(", Name : " + record.getName());
- System.out.println(", Age : " + record.getAge());
- }
- System.out.println("------ Updating record with ID = ------");
- studentJDBCTemplate.update(2, 20);
- }
- }
- import java.util.List;
- import com.springsource.tcserver.serviceability.request.DataSource;
- public interface StudentDAO {
- public void setDataSource(DataSource ds);
- public void create(String name, Integer age);
- public Student getStudent(Integer id);
- public List<Student> listStudents();
- public void delete(Integer id);
- public void update(Integer id, Integer age);
- }
- import java.util.List;
- import org.springframework.jdbc.core.JdbcTemplate;
- import com.springsource.tcserver.serviceability.request.DataSource;
- public class StudentJDBCTemplate implements StudentDAO {
- private DataSource dataSource;
- private JdbcTemplate jdbcTemplateObject;
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- this.jdbcTemplateObject = new JdbcTemplate(dataSource);
- }
- public void create(String name, Integer age) {
- String SQL = "insert into Student (name, age) values ( ?, ?)";
- jdbcTemplateObject.update(SQL, name, age);
- System.out.println("Created Record Name = " + name + " Age = " + age);
- return;
- }
- public Student getStudent(Integer id) {
- String SQL = "select * from Student where id = ?";
- Student student = jdbcTemplateObject.queryForObject(SQL,
- new Object[]{id}, new StudentMapper());
- return student;
- }
- public List <Student> listStudents() {
- String SQL = "select * from Student";
- List <Student> student = jdbcTemplateObject.query(SQL, new StudentMapper());
- return student;
- }
- public void delete(Integer id) {
- String SQL = "delete from Student where id = ?";
- jdbcTemplateObject.update(SQL, id);
- System.out.println("Deleted Record with ID = " + id );
- return;
- }
- public void update(Integer id, Integer age) {
- String SQL = "update Student set age = ? where id = ? ";
- jdbcTemplateObject.update(SQL, age, id);
- System.out.println("Updated Record with ID = " + id );
- return;
- }
- }
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.springframework.jdbc.core.RowMapper;
- public class StudentMapper implements RowMapper<Student>{
- public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
- Student student = new Student();
- student.setId(rs.getInt("id"));
- student.setName(rs.getString("name"));
- student.setAge(rs.getInt("age"));
- return student;
- }
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
- <!-- Initialization for data source -->
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
- <property name="username" value="root"/>
- <property name="password" value="password"/>
- </bean>
- <!-- Definition for studentJDBCTemplate bean -->
- <bean id="studentJDBCTemplate"
- class="com.mego.StudentJDBCTemplate">
- <property name="dataSource" ref="dataSource" />
- </bean>
- </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement