Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- JDBC Template -->
- <!-- Initialization for data source -->
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
- <property name="url" value="jdbc:oracle:thin:@localhost:1522:orcl" />
- <property name="username" value="hr" />
- <property name="password" value="hr" />
- </bean>
- <!-- Definition for studentJDBCTemplate bean -->
- <bean id="studentJDBCTemplate" class="spring.jdbctemplate.StudentJDBCTemplate">
- <property name="dataSource" ref="dataSource" />
- </bean>
- /////
- package spring.jdbctemplate;
- import java.util.List;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class JdbcTemplateDemo {
- 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(1,"Rajeev", 11);
- studentJDBCTemplate.create(2,"Kumar", 2);
- studentJDBCTemplate.create(3,"Mohan", 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 = 2 -----");
- studentJDBCTemplate.update(2, 20);
- System.out.println("----Listing Record with ID = 2 -----");
- Student student = studentJDBCTemplate.getStudent(2);
- System.out.print("ID : " + student.getId());
- System.out.print(", Name : " + student.getName());
- System.out.println(", Age : " + student.getAge());
- }
- }
Add Comment
Please, Sign In to add comment