Newsletter

Spring JdbcTemplate Select Query Examples

Spring » on Feb 11, 2012 { 9 Comments } By Sivateja

Let us see how to use spring JdbcTemplate select query

Files Required

  • SpringJdbcSelect.java
  • OurLogic.java
  • spconfig.xml

Directory Structure

SpringJdbcSelect.java

package java4s;

import java.util.Iterator;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

public class SpringJdbcSelect
{
	JdbcTemplate jt;
	public void setJt(JdbcTemplate jt)
	{
		this.jt = jt;
	}
	public void loadAll()
	{
		List l = jt.queryForList("select * from countries");
		Iterator it = l.iterator();
		while(it.hasNext())
		{
			Object o = it.next();
			System.out.println(o.toString());
		}
	}
}

OurLogic.java

package java4s;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class OurLogic
{
	public static void main(String args[])
	{
		Resource res = new ClassPathResource("spconfig.xml");
		BeanFactory factory = new XmlBeanFactory(res);
		SpringJdbcSelect jt =(SpringJdbcSelect)factory.getBean("id3");
		jt.loadAll();
	}
}

spconfig.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="id1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
  <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
  <property name="username" value="system"/>
  <property name="password" value="admin"/>
</bean>

<bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate">
     <constructor-arg>
        <ref bean="id1"/>
     </constructor-arg>    
</bean>
     
<bean id="id3" class="java4s.SpringJdbcSelect">
    <property name="jt">
       <ref bean="id2"/>
    </property>
</bean>

</beans>

Output:

Explanation

See the xml file.. actually our beam is id3, which needs JdbcTemplate to use the methods so i have given <ref bean=”id2″/>, and JdbcTemplate(id2) class need DriverManagerDataSource help so i have given <ref bean=”id1“/>, hope you got it.

​​

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

9 Responses to “Spring JdbcTemplate Select Query Examples”
  1. Raja says:

    In SpringJdbcSelect.java instead of iterating the list trough Iterator its better to use for-each loop then code will be like

      public void loadAll()
        {
          List l = jt.queryForList(“select * from countries”);
          for (Object o : l)
          System.out.println(o.toString());
        }
        }

  2. Java4s says:

    @Raja

    Yeah we can use either Iterator or Foreach, actually its depends, will not possible in all the cases 🙂

  3. 1.How can i connect with sql database please tell me…..
    2.how can i create tabe in mysql with Spring framework…

    Give me one-one example like oracle database

  4. Divya says:

    1.How to get the values from Login form and
    2.How to check that the person is valid user or not by using JdbcTemplate……. plz explain it with a small Login web application

  5. Harshitha says:

    Hi I am getting the foolowing error: Error creating bean with name ‘id3’

  6. Giri says:

    Check configuration file what u given for bean id.

    might be wrong name u given

  7. Sangeetha says:

    Hi, Please make sure your have added respective driver jar. ex : for db2 , we need to add “db2jcc-9.jar”

  8. Shiva says:

    Exception in thread “main” java.lang.NoSuchMethodError: org.springframework.core.CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(I)Ljava/util/Map;

    I am getting above error tell the process to resolve

  9. Jaydeep says:

    Hello sir,
    I want to connect this same program to mysql.
    So plz help me through which jar file i required and how to make this program

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.