Control Parameter Type In JdbcTemplate.Query : JdbcTemplate « Spring « Java

Control Parameter Type In JdbcTemplate.Query

File: context.xml

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

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value=""/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:."/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
    <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://dbhost-prospring-psql/prospring"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    <bean id="lobHandler" class="">
        <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>

    <bean id="nativeJdbcExtractor" class=""/>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>



import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
    DataSource dataSource = (DataSource) ac.getBean("dataSource");
    // DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    System.out.println(jdbcTemplate.query("select first_name from t_customer where id=?",
            new Object[] { 1L },
            new int[] { Types.INTEGER },
            new FirstColumnStringRowMapper()));

class FirstColumnStringRowMapper implements RowMapper {
  public Object mapRow(ResultSet resultSet, int i) throws SQLException {
    return resultSet.getString(1);

       3,656 k)

Related examples in the same category

1.Use queryForInt To Execute Aggregate Function
2.Use JdbcTemplate To Execute Delete Statement With Parameter
3.Use JdbcTemplate To Execute Aggregate Function: queryForLong
4.Use JdbcTemplate To Create Table
5.Pass DataSource to se JdbcTemplate
6.Use batchUpdate from JdbcTemplate To Execute Two UpdateStatements
7.UpdateStatement With Two Parameters
8.Execute Update and Delete Statements With JdbcTemplate
9.Query For Rowset
10.Query For Object, Return String
11.Query For Map
12.Query For List, Returns List Of Linked HashMap
13.Query For List
14.Pass Parameter As Object Array
15.Pass Object Array To Query Method In JdbcTemplate
16.Number Of Row Affected
17.Execute Delete Statement
18.Deal With TimeStamp