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

 

 








By ( Java/J2EE Developer & Web Designer )
Founder of Java4s.com. You can contact me at sivateja [at] java4s.com


Comments

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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
So, What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.