Can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Query Methods Of Spring JdbcTemplate With Examples

Spring » On Feb 10, 2012 1 Comment

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 ;)

 

 

 

What you are thinkig....

One Response 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 ???

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.