The update() method can complete the operations of inserting, updating and deleting data. In the JdbcTemplate class, a series of update() methods are provided, and the commonly used methods are shown in the following table:
method | illustrate |
---|---|
int update(String sql) | This method is the simplest overload of the update method, it directly executes the incoming SQL statement and returns the number of rows affected. |
int update(PreparedStatementCreatorpsc) | This method executes the statement returned from PreparedStatementCreator and then returns the number of rows affected. |
int update(String sql, PreparedStatementSetter pss) | This method sets the parameters in the SQL statement through the PreparedStatementSetter and returns the number of rows affected. |
int update(Stringsql,Object... args) | This method uses Object... to set parameters in the SQL statement, requiring that the parameters cannot be NULL, and returns the number of rows affected. |
foreground connection
This case extends the program function on the execute function
The addition, deletion and modification of DB are realized by the update() method. There are two commonly used overloaded methods for this method:
- public int update ( String sql)
- public int update ( String sql, Object… args)
The first parameter is the sql statement to be executed, and the second parameter is the dynamic parameter contained in the sql statement to be executed. Its return value is the number of records affected. Generally not.
case operation
1. Create an interface
import java.util.List;
public interface AccountDao {
// 添加
public int addAccount(Account account);
// 更新
public int updateAccount(Account account);
// 删除
public int deleteAccount(int id);
}
2. Create an implementation class
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class AccountDaoImpl implements AccountDao {
// 声明JdbcTemplate属性及其setter方法
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
// 添加账户
public int addAccount(Account account) {
// 定义SQL
String sql = "insert into account(username,balance) value(?,?)";
// 定义数组来存放SQL语句中的参数
Object[] obj = new Object[] { account.getUsername(), account.getBalance() };
// 执行添加操作,返回的是受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql, obj);
return num;
}
// 更新账户
public int updateAccount(Account account) {
// 定义SQL
String sql = "update account set username=?,balance=? where id = ?";
// 定义数组来存放SQL语句中的参数
Object[] params = new Object[] { account.getUsername(), account.getBalance(), account.getId() };
// 执行添加操作,返回的是受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql, params);
return num;
}
// 删除账户
public int deleteAccount(int id) {
// 定义SQL
String sql = "delete from account where id = ? ";
// 执行添加操作,返回的是受SQL语句影响的记录条数
int num = this.jdbcTemplate.update(sql, id);
return num;
}
}
3. Add dependencies
<!--定义id为accountDao的Bean-->
<bean id="accountDao" class="com.Example.jdbc.AccountDaoImpl">
<!-- 将jdbcTemplate注入到accountDao实例中 -->
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
Fourth, create a test class
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcTemplateTest_update {
public static void main(String[] args) {
// 加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取AccountDao实例
AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
// 创建Account对象,并向Account对象中添加数据
Account account = new Account();
account.setUsername("tom");
account.setBalance(1000.00);
// 执行addAccount()方法,并获取返回结果
int num = accountDao.addAccount(account);
if (num > 0) {
System.out.println("成功插入了" + num + "条数据!");
} else {
System.out.println("插入操作执行失败!");
}
// 修改数据
Account ud = new Account();
ud.setId(1);
ud.setUsername("tom");
ud.setBalance(2000.00);
// 执行updata方法并返回结果
int sum = accountDao.updateAccount(ud);
if (sum > 0) {
System.out.println("成功修改了" + sum + "条数据!");
} else {
System.out.println("修改操作执行失败!");
}
}
//执行findAccountById()方法
}
5. View the database
mysql> use spring;
Database changed
mysql> select * from account;
+----+-----------+---------+
| id | username | balance |
+----+-----------+---------+
| 1 | 孙悟空 | 100 |
| 2 | 唐僧 | 1000 |
| 3 | 猪八戒 | 2000 |
| 4 | 沙僧 | 5000 |
+----+-----------+---------+
4 rows in set (0.04 sec)
6. Program operation
7. Check the database again
mysql> select * from account;
+----+-----------+---------+
| id | username | balance |
+----+-----------+---------+
| 1 | tom | 2000 |
| 2 | 唐僧 | 1000 |
| 3 | 猪八戒 | 2000 |
| 4 | 沙僧 | 5000 |
| 5 | tom | 1000 |
+----+-----------+---------+
5 rows in set (0.00 sec)
The first data of account in the data table has been modified and the last data has been added