Home Tutorial Spring Spring3 Jdbc jdbctemplate select query

 
 

Share on Google+Share on Google+
jdbctemplate select query
Posted on: August 20, 2010 at 12:00 AM
Advertisement
In this Section, you will learn about using 'jdbcTemplate' for displaying value from database table.

Spring 'jdbctemplate' insert example

The 'JdbcTemplate' class is the core class in Spring JDBC core package. This class is responsible for executing SQL queries, releasing resource, updating statements, stored procedure calls, perform iteration over ResultSet & fetching of returned parameter . Due to this you need not taking care of closing connection. It also handles JDBC exception & convert them to more general, elaborative form. The hierarchy of exceptions is given in the package of "org.springframework.dao".

If you are using "JdbcTemplate"  in your code , you need to implement callback interfaces. The prepared statement is created by  "PreparedStatementCreator" callback interface to providing SQL and any necessary parameters. The "CallableStatementCreator" interface have the same function as "PreparedStatementCreator"  interface. The values from each row of a "ResultSet" is fetched by the "RowCallbackHandler" interface.

Example : In this Example ,Spring 'jdbctemplate' is used to display value from database table using select query.

Main.java

package net.roseindia;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.JdbcTemplate;

class Main {

public static void main(String args[]) throws Exception {

ApplicationContext ac = new ClassPathXmlApplicationContext(

"context.xml");

// DataSource dataSource = (DataSource) ac.getBean("dataSource");

DataSource dataSource = (DataSource) ac.getBean("dataSource");

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

System.out.println(jdbcTemplate.queryForList(

"select id from studentrecord", Long.class));

}

}

context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:lang="http://www.springframework.org/schema/lang"

xsi:schemaLocation=

"http://www.springframework.org/schema/beans

 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

http://www.springframework.org/schema/lang

http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://192.168.10.13:3306/ankdb"/>

<property name="username" value="root"/>

<property name="password" value="root"/>

</bean>

<bean id="lobHandler"

class="org.springframework.jdbc.support.lob.OracleLobHandler">

<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>

</bean>

<bean id="nativeJdbcExtractor"

class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="dataSource"/>

</bean>

</beans>

OUTPUT

Records in 'studentrecord' Database Table :

Output in Eclipse console after running code :

Download Source Code

Advertisement

Related Tags for jdbctemplate select query:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com