java实现批量插入

/**
* 批量插入
*
* @param tableName
* @param datas
* @return
*/
public boolean insertBatch(String tableName, List<Map<String, Object>> datas) {
PreparedStatement prepared = null;
Connection connect = new Mysql().getConnection();
try {
connect.setAutoCommit(false); // 设置手动提交
StringBuffer fieldString = new StringBuffer();
StringBuffer paraNo = new StringBuffer(); // 预处理字段“?”
List values = new ArrayList();
boolean flag = true;
for (int i = 0; i < datas.size(); i++) {
for (Object element : datas.get(i).keySet()) {
if (flag) {
fieldString.append("," + element);
paraNo.append(",?");
}
values.add(datas.get(i).get(element));
}
flag = false;

			if (prepared == null) {
				// 所有参数组成的数组
				String queryString = "INSERT INTO " + tableName + " (" + fieldString.toString().substring(1)
						+ ") VALUES (" + paraNo.substring(1) + ")";
				prepared = connect.prepareStatement(queryString);
			}
			// 设置对应参数值
			for (int j = 0; j < datas.get(i).size(); j++) {
				prepared.setObject(j + 1, values.get(j));
			}
			prepared.addBatch();
			if (i % 10000 == 0) {
				prepared.executeBatch();
				connect.commit();
			}
			values.clear();
		}
		prepared.executeBatch();
		connect.commit();
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException(e.getMessage());
	} finally {
		try {
			if (connect != null) {
				connect.close();
			}
			if (prepared != null) {
				prepared.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

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

/**

  • 单数据库模式 单数据库模式 需要扩展多数据库或使用主从数据库 在此处进行扩展指定连接数据库 同时修改数据库配置

*/
public class Mysql {
// 连接数据库的参数
private static String url = null;
private static String username = null;
private static String driver = null;
private static String passwd = null;

private static Properties propertie = null;
private static InputStream configStream = null;
/**
 * 加载驱动,只需要一次,用静态代码块 单数据库模式,需要扩展多数据库或使用主从数据库在此处进行扩展
 */
static {
	try {
		propertie = new Properties();
		configStream = Mysql.class.getClassLoader().getResourceAsStream("mysql.properties");
		propertie.load(configStream);

		url = propertie.getProperty("url");
		driver = propertie.getProperty("driver");
		username = propertie.getProperty("username");
		passwd = propertie.getProperty("password");

		Class.forName(driver);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			configStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		configStream = null;
	}

}

/**
 * 获取数据库链接
 * 
 * @return
 */
public Connection getConnection() {
	Connection con = null;
	try {
		con = DriverManager.getConnection(url, username, passwd);
	} catch (Exception e) {
		e.printStackTrace();
	}

	return con;
}

}

//调用方法

List<Map<String, Object>> datas = new ArrayList<>();
Map<String,Object> user = new HashMap<>();
user.put(字段,对应值);
datas.add(user);
insertBatch(表名, datas);

猜你喜欢

转载自blog.csdn.net/abcafdsgr123456789/article/details/90060125