Newsletter

Query Methods Of Spring JdbcTemplate With Examples

Spring » on Feb 10, 2012 { 2 Comments } By Sivateja

Let us see the query methods of spring JdbcTemplate

  • queryForInt()
  • queryForLong()
  • queryForObject()
  • queryForList()

and 1 or 2 more but these are highly using methods, if you want i will give you the total query methods list.. 🙂 don’t fear nothing is there light..

queryForInt()

  • Return type of queryForInt() method is int

Syntax:
int k = queryForInt(” static sql command “) ;

&

int k = queryForInt(” dynamic sql command “, Object array);

  • See we can pass either static,dynamic sql commands
  • We used to execute inserting rows, deleting rows bla bla as its return type is integer

queryForLong()

  • Return type of queryForLong() method is long

Syntax:
long k = queryForLong(” static sql command “) ;

&

long k = queryForLong(” dynamic sql command “, Object array);

  • See we can pass either static,dynamic sql commands
  • We used to find sum of salaries of the employees bla bla., as its return type if long

queryForObject()

  • Return type of queryForObject() method is object of Object class, we used to type cast later…

Ex:
Object o1 = queryForObject(” select sysdate from dual “, Date.class) ;
Object o2 = queryForObject(” select salary from emp”, Integer.class);
Object o3 = queryForObject(” select empName from emp”,String.class);

Date d = (Date)o1;
Integer i = (Integer)o2;
String s = (String)o3;

  • 🙂 see above 3 examples, i mean this queryForObject() will return the object of the type we called in the form of Object class object [ read this point slowly if its not clear 🙂 ]
  • We used to find sum of salaries of the employees bla bla., as its return type if long

queryForList()

  • Return type of queryForList() method is List object

Syntax:
List l = jt.queryForList(” static sql command “) ;

&

List l = jt.queryForList(” dynamic sql command “, Object array);

  • See we can pass either static,dynamic sql commands

So we are clear about all fundamentals for springJDBC let us start the applications 😉

 

 

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

2 Responses to “Query Methods Of Spring JdbcTemplate With Examples”
  1. hi
    good article ,i have a question
    ArrayList chasssisCodesList;
    try{
    chasssisCodesList = (ArrayList) queryForList(
    “select a,b,c from customer”, map);
    }catch(Exception e){}
    here chasssisCodesList is not initilized any where like new ArrayList(1);
    if a called chasssisCodesList.size(),do it will throw nullPointer ???

  2. manish kumar says:

    i want to check user is exists inside database or not using spring mvc and rowmapper.please help me

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.