JavaEE SpringJDBC——update()添加,修改,删除

update()方法可以完成插入,更新和删除数据的操作,在JdbcTemplate类中,提供了一系列的update()方法,

接下来通过一个用户管理的案例来演示update()方法的使用,具体步骤如下:

1、在chapter04项目下的com.itheima.jdk包中创建Account类,在该类中定义id,username,balance属性,以及对应的getter/setter方法,代码如下所示:

package com.itheima.jdbc;

public class Account {
	private Integer id;//账户id
	private String username;//用户名
	private Double balance;//账户余额

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Double getBalance() {
		return balance;
	}
	public void setBalance(Double balance) {
		this.balance = balance;
	}
	public String toString() {
		return "Account [id=" + id + "," + "username=" + username + ",balance=" + balance + "]";
	}
}

2、在com.itheima.jdbc包中,创建接口AccoutDao,并在接口中定义添加,更新,删除账户的方法;

package com.itheima.jdbc;

public interface AccountDao {

	//添加
	public int addAccount(Account account) ;
	//更新
	public int upAccount(Account account) ;
	//删除
	public int deleteAccount(int id) ;

}

3、在com.itheima.jdbc包中,创建AccountDao接口的实现类AccountDaoImpl,并在类中实现添加,删除,更新等操作

package com.itheima.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class AccountDaoImpl implements AccountDao {
	
	//声明JdbcTemplate属性及其setter方法
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
    //添加账户
	@Override
	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;
	}
	//更新账户
	@Override
	public int upAccount(Account account) {
		//定义SQL数据库操作语句
		String sql= "update account set username=?,balance=? where id=?";
		//定义数组来储存SQL语句中的参数
		Object obj =new Object[] {
				account.getUsername(),
				account.getBalance(),
				account.getId(),
		};
		//执行更新操作,返回的是受SQL语句影响的记录条数
	    int num=this.jdbcTemplate.update(sql, obj);
		return num;
	}

	//删除操作
	@Override
	public int deleteAccount(int id) {
		//定义SQL数据库操作语句
		String sql= "delete from account where id=?";
		//执行删除操作,返回的是受SQL语句影响的记录条数
	    int num=this.jdbcTemplate.update(sql, id);
		return num;
	}

}

4、在之前好的applicationContext.xml中(创建这个文件是在上一篇博客中点这里)定义一个id为accountDao的Bean,该Bean用于将jdbcTemplate注入到accountDao实例中,其代码如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
        <!-- 1.配置数据源 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <!-- 数据库驱动 -->
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <!-- 连接数据库的url -->
            <property name="url" value="jdbc:mysql://localhost/spring" />
            <!-- 连接数据库的用户名 -->
            <property name="username" value="root" />
            <!-- 连接数据库的用户密码 -->
            <property name="password" value="itcast" /> 
        </bean>
        <!-- 配置JDBC模板 -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!-- 默认使用数据源 -->
            <property name="dataSource" ref="dataSource" />
        </bean>
        <bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate" />
        </bean>
</beans>

重点是

<bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate" />
        </bean>

5、在测试类JdbcTemplate中添加一个测试方法addAccountTest(),该方法主要用于添加用户账户信息,具体代码如下:

@Test
	public  void addAccountTest() {
		ApplicationContext lizi = new ClassPathXmlApplicationContext("applicationContext.xml");
		AccountDao accountDao = (AccountDao)lizi.getBean("accountDao");
		 
		//创建Account对象,并向Account对象中添加数据
		Account account = new Account();
		account.setBalance(1000.00);
		account.setUsername("tom");
		
		//执行update方法,并获取返回结果
		int num=accountDao.addAccount(account);
		if(num!=0)System.out.println("创建成功了,插入了"+num+"条数据");
		else {
			System.out.println("创建失败了");
		}
	}

进行Junit4测试运行,运行结果如下所示:

 

 

从结果来看是创建成功了,此时我们查询一下accunt表,

 

可以看到account表中确实添加了一条信息;

 

 

6、执行完插入操作后接下来使用JdbcTemplate类的update()方法执行更新操作,在测试类JdbcTemplateTest中,添加一个测试方法upAccountTest(),其代码如下所示:

@Test
	public  void upzAccountTest() {
		ApplicationContext lizi = new ClassPathXmlApplicationContext("applicationContext.xml");
		AccountDao accountDao = (AccountDao)lizi.getBean("accountDao");
		 
		//创建Account对象,并向Account对象中添加数据
		Account account = new Account();
		account.setId(1);
		account.setBalance(1000.00);
		account.setUsername("tom");
		
		//执行upAccount方法,并获取返回结果
		int num=accountDao.upAccount(account);
		if(num!=0)System.out.println("成功修改了,插入了"+num+"条数据");
		else {
			System.out.println("修改失败了");
		}
	}

运行结果如下所示:

 

现在再来看一看account表中信息

 

从表中来看确实是修改了表中tom的账户余额

 

7、现在来演示删除操作,在测试类JdbcTemplateTest中,添加一个测试方法deleteAccountTest(),具体代码如下:

@Test
	public  void deleteAccountTest() {
		ApplicationContext lizi = new ClassPathXmlApplicationContext("applicationContext.xml");
		AccountDao accountDao = (AccountDao)lizi.getBean("accountDao");
		 
		//执行upAccount方法,删除id为1的账号,并获取返回结果
		int num=accountDao.deleteAccount(1);
		if(num!=0)System.out.println("成功删除了"+num+"条数据");
		else {
			System.out.println("删除失败了");
		}
	}

测试结果如图所示:

 

此时再次看看account表中的数据

 

发现为空了,说明删除操作成功了,

至此update()添加,修改,删除的操作全部演示完成了!

 

 

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值