Searching...
Tuesday, 1 October 2013

Spring JdbcTemplate Simple Example

23:53

Spring API provides JdbcTemplate to do database operations, it simplifies the use of JDBC creation and release resources. Here is one simple example to fetch database records using Spring JdbcTemplate.

Software Used

Java 1.7

Spring 3.0.5

MySql

1. Create a Database table

In this example we are creating a simple Student table and inserting one record.

--
-- Create schema springschema
--
CREATE DATABASE IF NOT EXISTS springschema;
USE springschema;
--
-- Definition of table `student`
--
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `name` varchar(10) NOT NULL,
  `standard` varchar(20) NOT NULL,
  PRIMARY KEY (`name`),
  UNIQUE KEY `name` (`name`),
  UNIQUE KEY `standard` (`standard`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `student`
--
INSERT INTO `student` (`name`,`standard`) VALUES 
 ('Raj','10th Standard');

2. Create a Maven project and add below dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.17</version>
    </dependency>
</dependencies>

3. Create a Java class to fetch database records

package com.pretech.jdbc;
import java.util.Iterator;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
public class SpringJdbc {
	JdbcTemplate template;
	public void setTemplate(JdbcTemplate template) {
		this.template = template;
	}
	public void getStudentDetails() {
		List studentList = template.queryForList("select * from student");
		Iterator it = studentList.iterator();
		while (it.hasNext()) {
			Object object = it.next();
			System.out.println(object.toString());
		}
	}
}

4. Create Spring configuration file (SpringConfig.xml)


In this file we have to configure below Beans definitions

Create data source bean, Create JdbcTemplate using data source, Inject JdbcTemplate to our SpringJdbc class
<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">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/springschema"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg>
            <ref bean="dataSource" />
        </constructor-arg>
    </bean>
    <bean id="springjdbc" class="com.pretech.jdbc.SpringJdbc">
        <property name="template">
            <ref bean="jdbctemplate" />
        </property>
    </bean>
</beans>

5. Create a Main class (SpringJdbcMain.java)

package com.pretech.jdbc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringJdbcMain {
	public static void main(String[] args) {
		Resource resource = new ClassPathResource("SpringConfig.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		SpringJdbc springjdbc = (SpringJdbc) factory.getBean("springjdbc");
		springjdbc.getStudentDetails();
	}
}

6.Output



INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
{name=Raj, standard=10th Standard}



Download SpringJdbcExample

0 comments: