1. 程式人生 > 實用技巧 >MYSQL 之 JDBC(三): 增刪改查(一)通過Statement執行更新操作

MYSQL 之 JDBC(三): 增刪改查(一)通過Statement執行更新操作

Statement測試

/**
 * 通過JDBC向指定的資料表中插入一條記錄
 * 1. Statement:用於執行sql語句的物件
 * 1.1 通過Connection的createStatement()方法來獲取
 * 1.2 通過executeUpdate(sql)可以執行SQL語句
 * 1.3 傳入的sql可以是insert, update或delete,但不能是select
 * 2. Connection、Statement都是應用程式和資料庫伺服器的連線資源。使用後一定要關閉。
 * 2.1 需要再finally中關閉
 * 3. 關閉順序:先獲取的後關,後獲取的先關
 
*/ public void testStatement() { Connection conn = null; Statement statement = null; try { // 1. 獲取資料庫連線 conn = getConnection2(); // 2. 準備插入的SQL語句 String sql = "insert into t_user (username, pwd) values('測試', 3352)"; String sql2 = "update t_user set username='傻瓜' where id = 20017";
// 3. 執行插入 // 3.1 獲取操作sql語句的Statement物件 statement = conn.createStatement(); // 3.2 呼叫Statement物件的executeUpdate(sql)執行SQL語句 statement.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { try { if (statement != null) {
// 4. 關閉Statement物件 statement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (statement != null) { // 5. 關閉Connection物件 conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } }

insert/update/delete封裝

/**
 * 通用的更新的方法:insert/update/delete
 * 版本1
 */
public void update(String sql){
    Connection conn = null;
    Statement statement = null;

    try {
        conn = getConnection2();
        statement = conn.createStatement();
        statement.executeUpdate(sql);
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

建立JDBC的工具類,封裝方法

  • 工具類

package com.litian.jdbc;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author: Li Tian
 * @contact: [email protected]
 * @software: IntelliJ IDEA
 * @file: JDBCUtils.java
 * @time: 2020/3/21 15:23
 * @desc: |操作JDBC的工具類,其中封裝了一些工具方法
 * Version1
 */

public class JDBCTools {

    /**
     * 關閉Statement和Connection的方法
     */
    public static void release(Statement statement, Connection conn) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void release(ResultSet rs, Statement statement, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (statement != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 1. 獲取連線的方法
     * 通過讀取配置檔案從資料庫伺服器獲取一個連線。
     *
     * @return
     */
    public static Connection getConnection() throws Exception {
        // 1. 準備連線資料庫的4個字串。
        // 1.1 建立Properties物件
        Properties properties = new Properties();
        // 1.2 獲取jdbc.properties對應的輸入流
        InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
        // 1.3 載入1.2對應的輸入流
        properties.load(in);
        // 1.4 具體決定user,password等4個字串。
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String jdbcUrl = properties.getProperty("jdbcUrl");
        String driver = properties.getProperty("driver");
        // 2. 載入資料庫驅動程式
        Class.forName(driver);
        // 3. 通過DriverManager的getConnection()方法獲取資料庫連線。
        return DriverManager.getConnection(jdbcUrl, user, password);
    }

}

修改後的insert/update/delete封裝

public void update(String sql) {
    Connection conn = null;
    Statement statement = null;

    try {
        conn = getConnection2();
        statement = conn.createStatement();
        statement.executeUpdate(sql);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        JDBCTools.release(statement, conn);
    }
}

————————————————
版權宣告:本文為CSDN博主「李英俊小朋友」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/qq_21579045/article/details/105386353