Get all updates of Java4s
Follow Us: Email 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 Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. 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.
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_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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