Newsletter

Spring JdbcTemplate Update() Insert Query Example

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

Spring JDBC, will see how to insert a record into database using JdbcTemplate class.  i mean using insert query in Spring JDBC using JdbcTemplate’s update() method.  Reason why i have used update() method for insert sql is update() method will returns the number of record(s) inserted.

Files Required

  • SpringJdbcInsert.java
  • OurLogic.java
  • spconfig.xml

Directory Structure

SpringJdbcInsert.java

package java4s;

import org.springframework.jdbc.core.JdbcTemplate;

public class SpringJdbcInsert
{
	JdbcTemplate jt;
	public void setJt(JdbcTemplate jt)
	{
		this.jt = jt;
	}
	public void insertRow()
	{
		int k = jt.update("insert into countries values(107,'US')");
		System.out.println(k+ " row(s) inserted");
	}
}

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);
		SpringJdbcInsert in =(SpringJdbcInsert)factory.getBean("id3");
		in.insertRow();
	}
}

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.SpringJdbcInsert">
           <property name="jt">
               <ref bean="id2"/>
           </property>
     </bean>

</beans>

Output:

1 row(s) inserted  [ Am not giving screen short, hope you will trust me 😉 ]
Note:  Even delete also same….

​​

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

8 Responses to “Spring JdbcTemplate Update() Insert Query Example”
  1. Nagendra says:

    Hi,sir
    ur site is excellent for developers.
    pls provide spring with hibernate,spring mvc,aop asap.
    How much time it will take to complete remaing tasks.

  2. Java4s says:

    @Nagendra

    Hi,

    We will post rest of spring modules as soon as possible, but we can’t specify exact time frame, hope you will understand.

    Specially AOP we are planning little big [ covering almost all consents ], be in touch with our newsletters and Facebook/twitter to get updates.

    Thank you.

  3. shyamkumar says:

    plz provide spring remaining modules(mvc,orm,AOP) and intigrations with struts,springs and hibernate .
    it is useful to us…

  4. ashok says:

    Excellent work, really helping to the programmers alot….

  5. Sumanth says:

    What can i say about this site.?? It simply wow..!!
    Great Work…!!

  6. houssam says:

    Maybe I m one year late, but this is an incredible site for learning spring in a simple way.

    thank you.

  7. chetan says:

    there is no need to create pojo class here? why?

  8. Jeski says:

    Hello sir, Will u please provide me jar files for jdbctemplated class related programs.
    Like above one

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.