Spring_10 JdbcTemplate 增, 刪,改

在spring中對(duì)jdbc進(jìn)行了封裝,在spring中我們使用JdbcTemplate 對(duì)數(shù)據(jù)庫(kù)進(jìn)行crud操作。
以下說(shuō)明示例都是基于MySQL數(shù)據(jù)庫(kù)

導(dǎo)入jar包

包名
commons-logging-1.1.3.jar
log4j-1.2.17.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
aopalliance-1.0.jar
aspectjweaver-1.8.9.jar
spring-aspects-4.2.4.RELEASE

先導(dǎo)入基礎(chǔ)jar

包名
commons-logging-1.1.3.jar
log4j-1.2.17.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
aopalliance-1.0.jar
aspectjweaver-1.8.9.jar
spring-aspects-4.2.4.RELEASE
包名
spring-jdbc-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar

導(dǎo)入數(shù)據(jù)庫(kù)jar

包名
spring-jdbc-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE.jar

上的這兩個(gè)包是spring 中的,不要忘記導(dǎo)入對(duì)應(yīng)數(shù)據(jù)庫(kù)的jar

設(shè)置數(shù)據(jù)庫(kù)信息

在DriverManagerDataSource 中設(shè)置數(shù)據(jù)庫(kù)驅(qū)動(dòng),連接地址,數(shù)據(jù)庫(kù)名,密碼等

DriverManagerDataSource dataSource = new DriverManagerDataSource();
//設(shè)置數(shù)據(jù)的驅(qū)動(dòng)
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//設(shè)置數(shù)據(jù)的連接地址
dataSource.setUrl("jdbc:mysql://35.160.96.200:3306/spring_db");
//設(shè)置用戶名
dataSource.setUsername("root");
//設(shè)置密碼
dataSource.setPassword("cfox");

創(chuàng)建Jdbc 模板

將jdbc設(shè)置信息通過(guò)jdbc進(jìn)行設(shè)置,同時(shí)通過(guò)jdbc模板對(duì)象對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作。

//創(chuàng)建一個(gè)jdbc 模板對(duì)象,注意 dataSource
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//sql語(yǔ)句
String str = "insert into user_info values(?,?)";
//進(jìn)行添加操作
int row = jdbcTemplate.update(str, "zhangsan",23);
System.out.println("----row:" + row);

增, 刪, 改

這塊不多語(yǔ)言介紹,直接上代碼把,很簡(jiǎn)單,都一樣,只是SQL語(yǔ)句同。

public class SpringJdbc {
    private DriverManagerDataSource dataSource;
    private JdbcTemplate template;
    public SpringJdbc() {
        dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/spring_db");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        template = new JdbcTemplate(dataSource);
    }
    
    public void add(){
        String sql = "insert into user_info values(?,?)";
        int row  = template.update(sql, "zhangwu", 25);
        System.out.println(row);
    }
    
    public void delete() {
        String sql = "delete from user_info where name=?";
        int row = template.update(sql, "zhangwu");
        System.out.println(row);
    }
    
    public void update() {
        String sql = "update user_info set age=? where name=?";
        int row = template.update(sql,80, "zhangwu");
        System.out.println(row);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,593評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評(píng)論 6 342
  • spring概念: 1.spring是開源的輕量級(jí)框架 2.spring核心主要倆部分:(1)aop:面向切面編程...
    暖熊熊閱讀 420評(píng)論 0 2
  • 此篇博客所有源碼均來(lái)自JDK 1.8 重入鎖ReentrantLock是排他鎖,排他鎖在同一時(shí)刻僅有一個(gè)線程可以進(jìn)...
    chenssy閱讀 1,416評(píng)論 0 9
  • 賣掉北京一套房子 拿一半錢去太平洋上買一座島 宣布建國(guó) 與中國(guó)建交 聲明堅(jiān)決擁護(hù)一個(gè)中國(guó)原則 就能在北京申請(qǐng)一塊地...
    愛(ài)神日記閱讀 391評(píng)論 0 0

友情鏈接更多精彩內(nèi)容