亲宝软件园·资讯

展开

JDBC批量操作 怎样使用JDBC实现工具类抽取

海之浪子 人气:0

这篇文章主要介绍了如何使用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();

  }
}

加载全部内容

相关教程
猜你喜欢
用户评论