tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Spring > JDBC > Query for List of Objects

Query for List of Objects 

Spring JDBC Framework simplifies the use of JDBC and helps to avoid common errors. The following example shows querying for typed list using JdbcTemplate class.


File Name  :  
/SpringJDBC001/conf/basic/applicationContext.xml 

File Name  :  
com/bethecoder/tutorials/spring3/tests/QueryForListOfObjectsTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.spring3.tests;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class QueryForListOfObjectsTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    /**
     * Initialize context and get the JdbcTemplate
     */
    ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSource = (DataSourceappContext.getBean("dataSource");
    JdbcTemplate template = new JdbcTemplate(dataSource);

    //The results will be mapped to a List (one entry for each row) 
    //of result objects, each of them matching the specified element type.
    List<String> statusList = 
      template.queryForList("select distinct(STATUS) from BUG_STAT", String.class);
    
    System.out.println(statusList);
    
    List<Integer> statusIDList = 
      template.queryForList("select ID from BUG_STAT", Integer.class);
    
    System.out.println(statusIDList);
  }

}
   

It gives the following output,
[RESOLVED, VERIFIED, CLOSED, REOPEN, ASSIGNED, NEW]
[1, 2, 3, 4, 5, 6]


                    Print

   



Books Recommendation

 
 
bl  br