JDBC批量操作

1、JDBC工具类抽取 上一篇做了JDBC的基本操作,但是获取连接及释放资源是比较重复的操作,可以抽取工具类而达到代码重用的目的 工程结构如图 JDBC工具类代码 db.properties driver=com.mysql...

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();

    }
}
  • 发表于 2019-05-23 23:40
  • 阅读 ( 156 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除