Update, Delete Statements With Jdbc Template : JdbcTemplate « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
   <bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">

   <property name="driverClassName">
       <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
   </property>

   <property name="url">
       <value>jdbc:odbc:test</value>
   </property>

   <property name="username">
       <value>root</value>
   </property>

   <property name="password">
       <value>sql</value>
   </property>

   </bean>

   <bean id="datacon" class="Dataconimpl">
      <property name="dataSource"><ref local="dataSource"/></property>
   </bean>
</beans>

File: Main.java

import javax.sql.DataSource;

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

public class Main {

  public static void main(String[] args) throws Exception {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    Dataconimpl bean1 = (Dataconimpl) factory.getBean("datacon");
    JdbcTemplate jt = new JdbcTemplate(bean1.dbcon());
 
    jt.execute("delete from table1  ");
    jt.execute("update table1 set table1.place='' ");

  }
}

class Dataconimpl implements Datacon {
  private DataSource dataSource;

  public void setDataSource(DataSource ds) {
    dataSource = ds;
  }

  public DataSource dbcon() {
    return dataSource;
  }
}

interface Datacon {
  public DataSource dbcon();
}
  Download:  Spring-UpdateDeleteStatementsWithJdbcTemplate.zip( 2,894 k)








28.21.JdbcTemplate
28.21.1.Use QueryForInt To Execute Aggregate Function
28.21.2.Use JdbcTemplate To Execute Aggregate Function
28.21.3.Use Jdbc Template To Create Table
28.21.4.Use batchUpdate To Execute Two Update Statements
28.21.5.Update, Delete Statements With Jdbc Template
28.21.6.Execute DeleteStatement
28.21.7.Query For Object, Return String
28.21.8.Query For Map
28.21.9.Query For List, Returns List Of LinkedHashMap
28.21.10.Query For List
28.21.11.Pass RowCallbackHandler Into Query Method In JdbcTemplate
28.21.12.Pass ResultSetExtractor To Query Method In JdbcTemplate
28.21.13.Pass Parameter As Object Array
28.21.14.Pass Object Array To Query Method In JdbcTemplate
28.21.15.Number Of Row Affected