{"id":460,"date":"2017-07-29T21:55:49","date_gmt":"2017-07-29T21:55:49","guid":{"rendered":"http:\/\/kaizen-koka.com\/?p=460"},"modified":"2018-03-21T14:14:45","modified_gmt":"2018-03-21T14:14:45","slug":"spring-jdbctemplate","status":"publish","type":"post","link":"https:\/\/kaizen-koka.com\/?p=460","title":{"rendered":"Spring Jdbctemplate"},"content":{"rendered":"<p>\n\thttps:\/\/docs.spring.io\/spring\/docs\/current\/spring-framework-reference\/data-access.html\n<\/p>\n<p>\n\tworking&nbsp;example:&nbsp;<a href=\"https:\/\/github.com\/teachkoka\/jdbctemplate\">https:\/\/github.com\/teachkoka\/jdbctemplate<\/a>\n<\/p>\n<p>\n\t<strong>application.property&nbsp;<\/strong>\n<\/p>\n<pre class=\"brush:java;\">\r\nstudent.db.driver = org.postgresql.Driver\r\nstudent.db.url = jdbc:postgresql:\/\/127.0.0.1:5432\/b6\r\nstudent.db.username = postgres\r\nstudent.db.password = abcd12345\r\n<\/pre>\n<p>\n\t<strong>Repository Class<\/strong>\n<\/p>\n<pre class=\"brush:java;\">\r\n@Repository\r\npublic class StudentRepository {\r\n&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp; &nbsp;public static final String INSERT_STUDENT_QUERY =&quot;INSERT INTO STUDENT(name) VALUES (?)&quot;;\r\n&nbsp;&nbsp; &nbsp;public static final String FIND_STUDENT_QUERY =&quot;SELECT * FROM STUDENT&quot;;\r\n&nbsp;&nbsp; &nbsp;\r\n&nbsp;&nbsp; &nbsp;@Autowired\r\n&nbsp;&nbsp; &nbsp;public JdbcTemplate jdbcTemplate;\r\n&nbsp;&nbsp; &nbsp;\r\n&nbsp; &nbsp; public void save(String name){\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;jdbcTemplate.update(INSERT_STUDENT_QUERY,name);\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;\r\n&nbsp; &nbsp; }\r\n&nbsp; &nbsp;&nbsp;\r\n&nbsp; &nbsp; public List&lt;String&gt; findAll(){\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;List&lt;Map&lt;String, Object&gt;&gt; &nbsp;rows= jdbcTemplate.queryForList(FIND_STUDENT_QUERY);\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;for(Map row:rows){\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;list.add((String)row.get(&quot;name&quot;));\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;}\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;return list;\r\n&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;\r\n&nbsp; &nbsp; }\r\n\r\n}\r\n<\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<strong>AppConfig.java<\/strong>\n<\/p>\n<pre class=\"brush:java;\">\r\nimport javax.sql.DataSource;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.context.MessageSource;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.context.annotation.PropertySource;\r\nimport org.springframework.context.support.ReloadableResourceBundleMessageSource;\r\nimport org.springframework.context.support.ResourceBundleMessageSource;\r\nimport org.springframework.core.env.Environment;\r\nimport org.springframework.jdbc.core.JdbcTemplate;\r\nimport org.springframework.jdbc.datasource.DriverManagerDataSource;\r\nimport org.springframework.web.servlet.config.annotation.EnableWebMvc;\r\nimport org.springframework.web.servlet.view.JstlView;\r\nimport org.springframework.web.servlet.view.UrlBasedViewResolver;\r\n\r\n@Configuration\r\n@ComponentScan(&quot;com.teach.koka.javaconfig&quot;)\r\n@EnableWebMvc\r\n@PropertySource(&quot;classpath:application.properties&quot;)\r\npublic class AppConfig {\r\n\r\n\t@Autowired\r\n\tEnvironment env;\r\n\t\r\n\t@Bean\r\n\tpublic UrlBasedViewResolver urlBasedViewResolver(){\r\n\t\tUrlBasedViewResolver viewResolver = new UrlBasedViewResolver();\r\n\t\tviewResolver.setPrefix(&quot;\/WEB-INF\/pages\/&quot;);\r\n\t\tviewResolver.setSuffix(&quot;.jsp&quot;);\r\n\t\tviewResolver.setViewClass(JstlView.class);\r\n\t\t\r\n\t\treturn viewResolver;\r\n\t\t\r\n\t}\r\n\t\r\n\t    @Bean\r\n\t    public MessageSource messageSource() {\r\n\t\t ReloadableResourceBundleMessageSource  messageSource = new ReloadableResourceBundleMessageSource();\r\n\t        messageSource.setBasenames(&quot;application&quot;);\r\n\t        messageSource.setDefaultEncoding(&quot;UTF-8&quot;);\r\n\t        return messageSource;\r\n\t    }\r\n\t    \r\n\t    \/**\r\n\t\t * An alternative to the DriverManager facility, a DataSource object is\r\n\t\t *  the preferred means of getting a connection.\r\n\t\t *\/\r\n\t\t<strong>@Bean\r\n\t\tpublic DataSource dataSource(){\r\n\t\t\tDriverManagerDataSource dataSource = new DriverManagerDataSource();\r\n\t\t\tdataSource.setDriverClassName(env.getRequiredProperty(&quot;student.db.driver&quot;));\r\n\t\t\tdataSource.setUrl(env.getRequiredProperty(&quot;student.db.url&quot;));\r\n\t\t\tdataSource.setUsername(env.getRequiredProperty(&quot;student.db.username&quot;));\r\n\t\t\tdataSource.setPassword(env.getRequiredProperty(&quot;student.db.password&quot;));\r\n\t\t\t\r\n\t\t\treturn dataSource;\r\n\t\t}<\/strong>\r\n\t\t\r\n\t\t<strong>@Bean\r\n\t\tpublic JdbcTemplate jdbcTemplate(){\r\n\t\t\t JdbcTemplate jdbcTemplate = new JdbcTemplate();\r\n\t\t\t jdbcTemplate.setDataSource(dataSource());\r\n\t\t\r\n\t\t\t return jdbcTemplate;\r\n\t\t}<\/strong>\r\n\t\r\n}\r\n<\/pre>\n<p>\n\t<strong>Controller class<\/strong>\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.teach.koka.javaconfig.jdbctemplate.controller;\r\n\r\nimport java.util.*;\r\n\r\nimport javax.validation.Valid;\r\n\r\n\r\n\r\n\r\n\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.ui.Model;\r\nimport org.springframework.validation.BindingResult;\r\nimport org.springframework.web.bind.annotation.ModelAttribute;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestMethod;\r\n\r\nimport com.teach.koka.javaconfig.jdbctemplate.model.Student;\r\nimport com.teach.koka.javaconfig.jdbctemplate.repository.StudentRepository;\r\n\r\n\r\n@Controller\r\n@RequestMapping(value=&quot;\/student&quot;)\r\npublic class StudentController {\r\n\r\n\t@Autowired\r\n\tStudentRepository repository;\r\n\t\r\n\t@RequestMapping(method=RequestMethod.GET)\r\n\tpublic String studentData(Model model){\r\n\t\tStudent student = new Student();\r\n\t\tmodel.addAttribute(&quot;student&quot;,student);\r\n\t\tmodel.addAttribute(&quot;message&quot;,&quot;Welcome...&quot;);\r\n\t\treturn &quot;student-login&quot;;\t\t\r\n\t}\r\n\t\r\n\t&nbsp;&nbsp; &nbsp;@RequestMapping(method=RequestMethod.POST)\r\n&nbsp;&nbsp; &nbsp;public String postStudentData(@ModelAttribute(&quot;student&quot;) @Valid Student student,BindingResult result, Model model){\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(result.hasErrors()){\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return &quot;student-login&quot;;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String text =&quot;Hey Welcome to MVC world...,&quot;+student.getName();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;repository.save(student.getName());\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;model.addAttribute(&quot;message&quot;,text);\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;List list = repository.findAll();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;model.addAttribute(&quot;list&quot;,list);\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return &quot;success&quot;;\r\n&nbsp;&nbsp; &nbsp;}\r\n}\r\n<\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<strong>Iterator on jsp<\/strong>\n<\/p>\n<p>\n\t<strong>&nbsp; <\/strong>\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;div&gt;\r\n&lt;c:forEach var=&quot;student&quot; items=&quot;${list}&quot;&gt;\r\n&nbsp;&lt;c:out value=&quot;${student}&quot;\/&gt;&lt;\/br&gt;\r\n&lt;\/c:forEach&gt;\r\n&nbsp; &lt;\/div&gt;<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/docs.spring.io\/spring\/docs\/current\/spring-framework-reference\/data-access.html working&nbsp;example:&nbsp;https:\/\/github.com\/teachkoka\/jdbctemplate application.property&nbsp; student.db.driver = org.postgresql.Driver student.db.url = jdbc:postgresql:\/\/127.0.0.1:5432\/b6 student.db.username = postgres student.db.password = abcd12345 Repository Class @Repository public class StudentRepository { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;public static final String INSERT_STUDENT_QUERY [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[2],"tags":[],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-technology"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p70lnf-7q","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=460"}],"version-history":[{"count":9,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/460\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}