jdbctemplate select query


 

jdbctemplate select query

In this Section, you will learn about using 'jdbcTemplate' for displaying value from database table.

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"

0

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

xsi:schemaLocation=

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

1

 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

2

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

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

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

3

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">

4

<bean id="dataSource"

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

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

5

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

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

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

6

</bean>

<bean id="lobHandler"

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

7

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

</bean>

8

<bean id="nativeJdbcExtractor"

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

9

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

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

</bean>

0

</beans>

OUTPUT

Records in 'studentrecord' Database Table :

1

Output in Eclipse console after running code :

2

Download Source Code

Ads