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 .:: | ||
Comments
8 Responses to “Spring JdbcTemplate Update() Insert Query Example”
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.
@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.
plz provide spring remaining modules(mvc,orm,AOP) and intigrations with struts,springs and hibernate .
it is useful to us…
Excellent work, really helping to the programmers alot….
What can i say about this site.?? It simply wow..!!
Great Work…!!
Maybe I m one year late, but this is an incredible site for learning spring in a simple way.
thank you.
there is no need to create pojo class here? why?
Hello sir, Will u please provide me jar files for jdbctemplated class related programs.
Like above one