用的maven
导入spring-jdbc和spring-tx坐标
创建数据库表和实体
大概写一下吧,
SQL语句我在这不写了 大概就是这个意思
public class Account {
private String name;//姓名
private double money;//钱
public Account() {
}
public Account(String name, double money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"name='" + name + '\'' +
", money=" + money +
'}';
}
}
创建JdbcTemplate对象
执行数据库操作
public class JdbcTemplateText {
@Test//测试JdbcTemplate开发步骤
public void text() throws Exception {
//创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("root");
JdbcTemplate template = new JdbcTemplate();
//设置数据源 知道数据在哪
template.setDataSource(dataSource);
//创建数据源对象
int row = template.update("insert into account values(?,?)", "hehe", 5000);
System.out.println(row);
}
}