Spring JdbcTemplate Select Query Example

Let's see an example of spring JdbcTemplate select query:

Files Required

  • SpringSelectDemo.java
  • Spconfig.xml
  • Test.java

SpringSelectDemo.java

package java8s;
import java.util.*;
import org.springframework.jdbc.core.JdbcTemplate;
public class SpringSelectDemo {
    JdbcTemplate jt;
    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
    public void show() {
        List l = jt.queryForList("select * from students");
        Iterator it = l.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            System.out.println(obj.toString());
        }
    }
}

spconfig.xml

<< ? xml version = "1.0"
encoding = "UTF-8" ? >
    <
    beans
xmlns = "http://www.springframework.org/schema/beans"
xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns: p = "http://www.springframework.org/schema/p"
xsi: schemaLocation = "http://www.springframework.org/schema/beans
http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <
    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 = "oracle" / >
    <
    /bean> <
    bean id = "id2"
class = "org.springframework.jdbc.core.JdbcTemplate" >
    <
    constructor - arg >
    <
    ref bean = "id1" / >
    <
    /constructor-arg> <
    /bean> <
    bean id = "id3"
class = "java8s.SpringSelectDemo" >
    <
    property name = "jt" >
    <
    ref bean = "id2" / >
    <
    /property> <
    /bean> <
    /beans>

Test.java

package java8s;
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 Test {
    public static void main(String[] args) {
        Resource res = new ClassPathResource("spconfig.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        SpringSelectDemo jt = (SpringSelectDemo) factory.getBean("id3");
        jt.show();
    }
}

 

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment