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

Query for Int 

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


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

File Name  :  
com/bethecoder/tutorials/spring3/tests/QueryForIntTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests;

import javax.sql.DataSource;

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

public class QueryForIntTest {

  /**
   @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);

    int rowCount = template.queryForInt("select count(*) from BUG_STAT");
    System.out.println("Row count : " + rowCount);
    
    int totalBugCount = template.queryForInt("select sum(COUNT) from BUG_STAT");
    System.out.println("Total bug count : " + totalBugCount);
    
    String statusToCheck = "RESOLVED";
    int bugCount = template.queryForInt(
        "select COUNT from BUG_STAT where STATUS = ? ", statusToCheck);
    System.out.println(statusToCheck + " bug count : " + bugCount);
  }

}
   

It gives the following output,
Row count : 6
Total bug count : 60
RESOLVED bug count : 16



 
  


  
bl  br