Example of spring JdbcTemplate class

Let we will assume that we have user table that will create in Oracle10gXE database environment.

create table user
(
id number(10),
name varchar2(100),
address varchar2(100)
);

User.java

Now we will create a class User containing 3 properties with constructors and setter and getters as follows:

package java8s;
public class User
{
private int id;
private String name;
private String address;
//no-arg and parameterized constructors
//getters and setters method
}

UserDao.java

It contains one property jt and three methods saveUser(), updateUser() and deleteUser().

Package java8s;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDao {
    private JdbcTemplate jt;
    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
    public int saveUser(User e) {
        String query = "insert into employee values( '" + e.getId() + "','" + e.getName() + "','" + e.getAddress() + "')";
        return jt.update(query);
    }
    public int updateUser(User e) {
        String query = "update employee set name='" + e.getName() + "',salary='" + e.getAddress() + "' where id='" + e.getId() + "' ";
        return jt.update(query);
    }
    public int deleteUser(User e) {
        String query = "delete from user where id='" + e.getId() + "' ";
        return jt.update(query);
    }
}

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.DriverManagerDataSourc>
    <
    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 = "jt"
class = "org.springframework.jdbc.core.JdbcTemplate" >
    <
    property name = "dataSource"
ref = "id1" > < /property> <
    /bean> <
    bean id = "udao"
class = "java8s.UserDao" >
    <
    property name = "jt"
ref = "jt" > < /property> <
    /bean> <
    /beans>

ClientLogic.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 ClientLogic {
    public static void main(String args[]) {
        Resource res = new ClassPathResource("spconfig.xml");
        BeanFactory factory = new XmlBeanFactory(res);
        UserDao ud = (UserDao) res.getBean("udao");
        int i = ud.saveUser(new Employee(101, "Akshay", "BBSR"));
        System.out.println(i);
        // int i=ud.saveUser(new Employee(102,"Mona","Balosore"));
        // System.out.println(i);
        // User u=new User();
        // u.setId(101);
        // int i=ud.deleteUser(u);
        // System.out.println(i);
    }
}

 

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