JdbcTemplate基本使用


用的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);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM