1. 程式人生 > >JavaEE SpringJDBC——update()新增,修改,刪除

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()新增,修改,刪除的操作全部演示完成了!