JDBC批量操作


1、JDBC工具類抽取

上一篇做了JDBC的基本操作,但是獲取連接及釋放資源是比較重復的操作,可以抽取工具類而達到代碼重用的目的
工程結構如圖

JDBC工具類代碼
db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.47.151:3306/web?useUnicode=true&characterEncoding=utf8
username=root
password=root

JDBCUtils.java

package com.rookie.bigdata.util;

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

/**
 * Created by dell on 2019/5/22.
 */
package com.rookie.bigdata.util;

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

/**
 * Created by dell on 2019/5/22.
 */
public class JDBCUtils {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //    //靜態代碼塊加載配置文件信息
//    static {
//        ResourceBundle db = ResourceBundle.getBundle("db");
//        driver = db.getString("driver");
//        url = db.getString("url");
//        username = db.getString("username");
//        password = db.getString("password");
//    }
    //靜態代碼塊加載配置文件信息
    static {
        try {
            //獲取類加載器
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            //通過類加載器的方法獲取一個輸入流
            InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            //獲取相關參數的值
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 獲取連接
     *
     * @return
     */
    public static Connection getConnection() {

        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 釋放資源
     * @param conn
     * @param pstmt
     * @param rs
     */
    public static void relase(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}

2、批量插入數據

package com.rookie.bigdata;

import com.rookie.bigdata.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * CREATE TABLE `user` (
 * `USERNAME` varchar(30) DEFAULT NULL COMMENT '用戶名',
 * `PASSWORD` varchar(10) DEFAULT NULL COMMENT '密碼'
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 */
public class JDBCBatch {
    public static void main(String[] args) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        //設置自動提交關閉
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USER VALUES (?,?)");

        for (int i = 1; i <= 5000; i++) {
            preparedStatement.setString(1, "張三" + i);
            preparedStatement.setString(2, "123" + i);
            preparedStatement.addBatch();
            if (i % 1000 == 0) {
                preparedStatement.executeUpdate();
                connection.commit();
                preparedStatement.clearBatch();
            }
        }
        preparedStatement.executeUpdate();
        connection.commit();
        preparedStatement.clearBatch();

    }
}


免責聲明!

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



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