//创建数据库:
create database spring_day02;
use spring_day02;//创建表:
create table account(
id int primary key auto_increment,
name varchar(40),
money float)character set utf8 collate utf8_general_ci;
publicstaticvoidmain(String[] args){
//1.获取Spring容器
ApplicationContext ac =newClassPathXmlApplicationContext("bean.xml");//2.根据id获取JdbcTemplate对象
JdbcTemplate jt =(JdbcTemplate) ac.getBean("jdbcTemplate");//3.执行增加操作
jt.update("insert into account(name,money)values('eee',3400)");}
删除操作
publicstaticvoidmain(String[] args){
//1.获取Spring容器
ApplicationContext ac =newClassPathXmlApplicationContext("bean.xml");//2.根据id获取JdbcTemplate对象
JdbcTemplate jt =(JdbcTemplate) ac.getBean("jdbcTemplate");//3.执行删除操作
jt.update("delete from account where id = ?",2);}
更新操作
publicstaticvoidmain(String[] args){
//1.获取Spring容器
ApplicationContext ac =newClassPathXmlApplicationContext("bean.xml");//2.根据id获取JdbcTemplate对象
JdbcTemplate jt =(JdbcTemplate) ac.getBean("jdbcTemplate");//3.执行更新操作
jt.update("update account set name =?,money = ? where id = ?","wanglaowu",5000,1);}
public List<Account>findAllAccounts(){
List<Account> accounts = jdbcTemplate.query("select * from account",newBeanPropertyRowMapper<Account>(Account.class));return accounts;}
2.查询一条记录
public Account findAccountById(Integer accountId){
List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",newBeanPropertyRowMapper<Account>(Account.class), accountId);return accounts.isEmpty()? null : accounts.get(0);}