Advertisement
Guest User

Untitled

a guest
May 18th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. import java.util.List;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class MainApp {
  7. public static void main(String[] args) {
  8. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  9.  
  10. StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
  11.  
  12. System.out.println("------- Records Creation ------");
  13. studentJDBCTemplate.create("Zara", 11);
  14. studentJDBCTemplate.create("Nuha", 2);
  15. studentJDBCTemplate.create("Ayan", 15);
  16.  
  17. System.out.println("------- Listing Multiple Records -------");
  18. List<Student> students = studentJDBCTemplate.listStudents();
  19. for (Student record : students) {
  20. System.out.print("ID : " + record.getId());
  21. System.out.print(", Name : " + record.getName());
  22. System.out.println(", Age : " + record.getAge());
  23. }
  24.  
  25. System.out.println("------ Updating record with ID = ------");
  26. studentJDBCTemplate.update(2, 20);
  27.  
  28.  
  29. }
  30. }
  31.  
  32. import java.util.List;
  33.  
  34. import org.springframework.context.ApplicationContext;
  35. import org.springframework.context.support.ClassPathXmlApplicationContext;
  36.  
  37. public class MainApp {
  38. public static void main(String[] args) {
  39. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
  40.  
  41. StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
  42.  
  43. System.out.println("------- Records Creation ------");
  44. studentJDBCTemplate.create("Zara", 11);
  45. studentJDBCTemplate.create("Nuha", 2);
  46. studentJDBCTemplate.create("Ayan", 15);
  47.  
  48. System.out.println("------- Listing Multiple Records -------");
  49. List<Student> students = studentJDBCTemplate.listStudents();
  50. for (Student record : students) {
  51. System.out.print("ID : " + record.getId());
  52. System.out.print(", Name : " + record.getName());
  53. System.out.println(", Age : " + record.getAge());
  54. }
  55.  
  56. System.out.println("------ Updating record with ID = ------");
  57. studentJDBCTemplate.update(2, 20);
  58.  
  59.  
  60. }
  61. }
  62.  
  63. import java.util.List;
  64.  
  65. import com.springsource.tcserver.serviceability.request.DataSource;
  66.  
  67. public interface StudentDAO {
  68.  
  69. public void setDataSource(DataSource ds);
  70.  
  71. public void create(String name, Integer age);
  72.  
  73. public Student getStudent(Integer id);
  74.  
  75. public List<Student> listStudents();
  76.  
  77. public void delete(Integer id);
  78.  
  79. public void update(Integer id, Integer age);
  80. }
  81.  
  82. import java.util.List;
  83.  
  84. import org.springframework.jdbc.core.JdbcTemplate;
  85.  
  86. import com.springsource.tcserver.serviceability.request.DataSource;
  87.  
  88. public class StudentJDBCTemplate implements StudentDAO {
  89. private DataSource dataSource;
  90. private JdbcTemplate jdbcTemplateObject;
  91.  
  92. public void setDataSource(DataSource dataSource) {
  93. this.dataSource = dataSource;
  94. this.jdbcTemplateObject = new JdbcTemplate(dataSource);
  95. }
  96.  
  97. public void create(String name, Integer age) {
  98. String SQL = "insert into Student (name, age) values ( ?, ?)";
  99.  
  100. jdbcTemplateObject.update(SQL, name, age);
  101. System.out.println("Created Record Name = " + name + " Age = " + age);
  102. return;
  103. }
  104.  
  105. public Student getStudent(Integer id) {
  106. String SQL = "select * from Student where id = ?";
  107. Student student = jdbcTemplateObject.queryForObject(SQL,
  108. new Object[]{id}, new StudentMapper());
  109.  
  110. return student;
  111. }
  112.  
  113. public List <Student> listStudents() {
  114. String SQL = "select * from Student";
  115. List <Student> student = jdbcTemplateObject.query(SQL, new StudentMapper());
  116.  
  117. return student;
  118. }
  119.  
  120. public void delete(Integer id) {
  121. String SQL = "delete from Student where id = ?";
  122. jdbcTemplateObject.update(SQL, id);
  123. System.out.println("Deleted Record with ID = " + id );
  124.  
  125. return;
  126. }
  127.  
  128. public void update(Integer id, Integer age) {
  129. String SQL = "update Student set age = ? where id = ? ";
  130. jdbcTemplateObject.update(SQL, age, id);
  131. System.out.println("Updated Record with ID = " + id );
  132.  
  133. return;
  134. }
  135. }
  136.  
  137. import java.sql.ResultSet;
  138. import java.sql.SQLException;
  139.  
  140. import org.springframework.jdbc.core.RowMapper;
  141.  
  142. public class StudentMapper implements RowMapper<Student>{
  143. public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
  144. Student student = new Student();
  145. student.setId(rs.getInt("id"));
  146. student.setName(rs.getString("name"));
  147. student.setAge(rs.getInt("age"));
  148. return student;
  149. }
  150. }
  151.  
  152. <beans xmlns="http://www.springframework.org/schema/beans"
  153. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  154. xsi:schemaLocation="http://www.springframework.org/schema/beans
  155. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
  156.  
  157. <!-- Initialization for data source -->
  158. <bean id="dataSource"
  159. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  160. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  161. <property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
  162. <property name="username" value="root"/>
  163. <property name="password" value="password"/>
  164. </bean>
  165.  
  166. <!-- Definition for studentJDBCTemplate bean -->
  167. <bean id="studentJDBCTemplate"
  168. class="com.mego.StudentJDBCTemplate">
  169. <property name="dataSource" ref="dataSource" />
  170. </bean>
  171.  
  172. </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement