欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中JDBC增刪改查操作詳解

 更新時(shí)間:2021年12月16日 09:15:40   作者:Michelhjx  
大家好,本篇文章主要講的是java中JDBC增刪改查操作詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽

前言

? ? ? ? 在上一篇博客我們介紹了JDBC的概念以及連接數(shù)據(jù)庫的五種方式JDBC概述及數(shù)據(jù)庫連接方式(數(shù)據(jù)庫連接方式推薦使用第五種),那么我們既然連接上數(shù)據(jù)庫了,那就要對數(shù)據(jù)進(jìn)行操作了,那么這一篇我們就來介紹常規(guī)的增刪改 查操作。

??? ?

我們先看一遍步驟:? ??

這里不推薦使用Statement,兩個(gè)原因:

? ? ? ? ? ? ? ? 1.存在拼串操作,繁瑣。

? ? ? ? ? ? ? ? 2.存在sql注入問題。

? ? ? ? 那我們使用誰來代替Statement呢?

一、增刪改操作

? ? ? ? 操作前,我們先解決上面的問題,我們使用PreparedStatement來代替Statement。

1.1 PreparedStatement介紹

可以通過調(diào)用 Connection 對象的 preparedStatement(String sql) 方法獲取 PreparedStatement 對象

PreparedStatement 接口是 Statement 的子接口,它表示一條預(yù)編譯過的 SQL 語句

PreparedStatement 對象所代表的 SQL 語句中的參數(shù)用問號(?)來表示,調(diào)用 PreparedStatement 對象的 setXxx() 方法來設(shè)置這些參數(shù). setXxx() 方法有兩個(gè)參數(shù),第一個(gè)參數(shù)是要設(shè)置的 SQL 語句中的參數(shù)的索引(從 1 開始),第二個(gè)是設(shè)置的 SQL 語句中的參數(shù)的值

?優(yōu)點(diǎn):

解決字符串拼串問題,防止sql注入提高性能? ? ? ?

?1.2 增刪改操作

? ? ? ? 這里給出一個(gè)針對不同表的通用增刪改操作,拿著直接用就可以了。

//通用增刪改操作
    public boolean updateInfo(String sql,Object...args){
        Connection conn = null;
        PreparedStatement ps = null;
        boolean b = true;
        try {
            //1.建立連接
            conn = JDBCUtils.getConnection();
            //2.預(yù)編譯sql語句
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i <args.length ; i++) {
                ps.setObject(i+1,args[i]);
            }
            //4.執(zhí)行操作
            b = ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.關(guān)閉資源
            JDBCUtils.close(conn,ps);
        }
        return b;
    }

? ? ? ? ?是不是復(fù)制使用會報(bào)錯,這是因?yàn)椴┲靼褦?shù)據(jù)庫連接和關(guān)閉資源也封裝成了一個(gè)類JDBCUtils。

? ? ? ? 下面給出這個(gè)類的具體代碼:

? ? ? ? 配置文件就自己寫吧,畢竟每個(gè)人的數(shù)據(jù)庫都不一樣。以及該導(dǎo)入的包自己動動手就行了。

public class JDBCUtils {
    //連接
    public static Connection getConnection(){
        Connection conn = null;
        try {
            //1.加載配置文件
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
            Properties prop = new Properties();
            prop.load(is);
 
            //2.讀取配置文件
            String user = prop.getProperty("user");
            String password = prop.getProperty("password");
            String url = prop.getProperty("url");
            String driverClass = prop.getProperty("driverClass");
 
            //3.加載驅(qū)動
            Class.forName(driverClass);
 
            //4.建立連接
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
        return conn;
    }
 
    //關(guān)閉
    public static void close(Connection conn, PreparedStatement ps){
        try {
            if (conn != null){
                conn.close();
            }
            if (ps != null){
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //關(guān)閉帶resultset
    public static void close(Connection conn, PreparedStatement ps , ResultSet rs){
        try {
            if (conn != null){
                conn.close();
            }
            if (ps != null){
                ps.close();
            }
            if (rs != null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

? 1.3 測試

? ? ? ? 我們來測試一下上面的代碼

? ? ? ? 這里有一張order表,里面有初始數(shù)據(jù):

???????????????????????

? ? ? ? ?然后我們通過上面提供的方法給他增加一條數(shù)據(jù):

public class Test {
    public static void main(String[] args) {
        Operation operation = new Operation();
        String insertSql = "INSERT INTO `order`(order_id,order_name,order_date) VALUES (?,?,?)";
        boolean b = operation.updateInfo(insertSql, 3, "CC", new Date(12312132));
        if (!b){
            System.out.println("操作成功!");
        }else {
            System.out.println("操作失??!");
        }
    }
}

? ? ? ? 控制臺打印輸出:

????????

? ? ? ? ?那么是不是已經(jīng)插入成功了呢,我們?nèi)?shù)據(jù)庫中看一下:

? ? ? ? ? 我們?SELECT * FROM `order`;

? ? ? ??

? ? ? ? 看來我們確實(shí)成功了!所以我們給出的方法還是沒有問題。

二、查操作

? ? ? ? 接下來我們就是查操作了。

2.1?通用對不同表進(jìn)行一條數(shù)據(jù)查詢操作

public  <T> T doQuery(Class<T> clazz,String sql,Object...args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1.建立連接
            conn = JDBCUtils.getConnection();
            //2.預(yù)編譯sql語句
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i <args.length ; i++) {
                ps.setObject(i+1,args[i]);
            }
            //4.得到結(jié)果集ResultSet
            rs = ps.executeQuery();
            //5.得到結(jié)果集元數(shù)據(jù)
            ResultSetMetaData rsdm = rs.getMetaData();
 
            //6.通過rs得到列值,rsdm得到列名和列數(shù)
            //得到列數(shù)
            int columnCount = rsdm.getColumnCount();
            if (rs.next()){
                T t = clazz.getDeclaredConstructor().newInstance();
                for (int i = 0; i <columnCount ; i++) {
                    //得到列值
                    Object columnvalue = rs.getObject(i + 1);
                    //得到列的別名
                    String columnLabel = rsdm.getColumnLabel(i + 1);
 
                    //通過反射給對象賦值
                    Field field = clazz.getDeclaredField(columnLabel);
                    //暴力反射
                    field.setAccessible(true);
                    field.set(t,columnvalue);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //關(guān)閉資源
            JDBCUtils.close(conn,ps,rs);
        }
        return null;
    }

? ? ? ? 老樣子,測試!

public class Test {
    public static void main(String[] args) {
        Operation operation = new Operation();
        String sql = "SELECT order_id orderId,order_name orderName,order_date orderDate FROM `order` WHERE order_id = ?;";
        Order order = operation.doQuery(Order.class, sql, 2);
        System.out.println(order);
    }
}

?控制臺輸出:

????????

2.2?通用對不同表進(jìn)行多條數(shù)據(jù)查詢操作

public <T> List doQueryMore(Class<T> clazz,String sql,Object...args){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //1.建立連接
            conn = JDBCUtils.getConnection();
            //2.預(yù)編譯sql語句
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i <args.length ; i++) {
                ps.setObject(i+1,args[i]);
            }
            //4.得到結(jié)果集ResultSet
            rs = ps.executeQuery();
            //5.得到結(jié)果集元數(shù)據(jù)
            ResultSetMetaData rsdm = rs.getMetaData();
 
            //6.通過rs得到列值,rsdm得到列名和列數(shù)
            //得到列數(shù)
            int columnCount = rsdm.getColumnCount();
            //創(chuàng)建集合
            ArrayList<T> list = new ArrayList<>();
            while (rs.next()){
                T t = clazz.getDeclaredConstructor().newInstance();
                for (int i = 0; i <columnCount ; i++) {
                    //得到列值
                    Object columnvalue = rs.getObject(i + 1);
                    //得到列的別名
                    String columnLabel = rsdm.getColumnLabel(i + 1);
 
                    //通過反射給對象賦值
                    Field field = clazz.getDeclaredField(columnLabel);
                    //暴力反射
                    field.setAccessible(true);
                    field.set(t,columnvalue);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //關(guān)閉資源
            JDBCUtils.close(conn,ps,rs);
        }
        return null;
    }

? ? ? ? ?這一個(gè)測試就交給各位吧!

總結(jié)

到此這篇關(guān)于java中JDBC增刪改查操作詳解的文章就介紹到這了,更多相關(guān)java JDBC增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot啟動停止命令的.sh腳本編寫方式

    Springboot啟動停止命令的.sh腳本編寫方式

    這篇文章主要介紹了Springboot啟動停止命令的.sh腳本編寫方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 如何在springMVC的controller中獲取request

    如何在springMVC的controller中獲取request

    這篇文章主要介紹了如何在springMVC的controller中獲取request,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot四種讀取properties文件的方式(小結(jié))

    SpringBoot四種讀取properties文件的方式(小結(jié))

    這篇文章主要介紹了SpringBoot四種讀取properties文件的方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 關(guān)于Springboot打成JAR包后讀取外部配置文件的問題

    關(guān)于Springboot打成JAR包后讀取外部配置文件的問題

    這篇文章主要介紹了關(guān)于Springboot打成JAR包后讀取外部配置文件的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • springboot控制層傳遞參數(shù)為非必填值的操作

    springboot控制層傳遞參數(shù)為非必填值的操作

    這篇文章主要介紹了springboot控制層傳遞參數(shù)為非必填值的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringCloud-Hystrix實(shí)現(xiàn)原理總結(jié)

    SpringCloud-Hystrix實(shí)現(xiàn)原理總結(jié)

    通過hystrix可以解決雪崩效應(yīng)問題,它提供了資源隔離、降級機(jī)制、融斷、緩存等功能。接下來通過本文給大家分享SpringCloud-Hystrix實(shí)現(xiàn)原理,感興趣的朋友一起看看吧
    2021-05-05
  • Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn)代碼實(shí)例

    Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • IDEA的Mybatis Log Plugin插件配置和使用詳解

    IDEA的Mybatis Log Plugin插件配置和使用詳解

    這篇文章主要介紹了IDEA的Mybatis Log Plugin插件配置和使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java簡單工廠模式詳細(xì)解釋

    Java簡單工廠模式詳細(xì)解釋

    本文主要介紹了JAVA簡單工廠模式(從現(xiàn)實(shí)生活角度理解代碼原理)的相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2021-11-11
  • SpringBoot中注解@AliasFor的使用詳解

    SpringBoot中注解@AliasFor的使用詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot中注解@AliasFor的用法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下
    2022-05-05

最新評論