zoukankan      html  css  js  c++  java
  • 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();
    
        }
    }
    
    
  • 相关阅读:
    创建Android项目时出错——No resource found that matches the given name 'Theme.AppCompat.Light'
    Java、Android 开发环境搭建
    tomcat7的安装与配置、及Servlet部署
    让实体对象自行决定留存操作类型(增删改)
    聚合体
    PowerDesigner15中定义varbinary(max)列
    Pig limit用法举例
    Pig join用法举例
    Pig distinct用法举例
    Pig group用法举例
  • 原文地址:https://www.cnblogs.com/haizhilangzi/p/10903678.html
Copyright © 2011-2022 走看看