package read;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;public class DBconn {int bufferSize=20*1024*1024;ArrayList column3string=new ArrayList();ArrayList column13string=new ArrayList();ArrayList test2col1 = new ArrayList();ArrayList test2col2 = new ArrayList();ArrayList test2col3 = new ArrayList();String driver = "com.mysql.jdbc.Driver";static String dbName="test";static String password="6983124ACD";static String userName="root";static String url="jdbc:mysql://localhost:3306/" + dbName;static String sql= "select * from workinfo";Connection conn=null;public static Connection getConnection() {String encoding = "utf-8";Connection conn = null;try {Class.forName("com.mysql.jdbc.Driver");conn=DriverManager.getConnection(url,userName,password);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public void readFile(String filename) throws FileNotFoundException{File file = new File(filename);if (file.isFile() && file.exists()) {BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader, bufferSize);String lineTXT = null;PreparedStatement pstmt = null;Connection conn = getConnection();//String sql = "insert into workinfo(column3,column13) values(1,2)(2,3)(4,5)";String sql = "insert into workinfo(column3,column13) values(?,?)";try {while((lineTXT=bufferedReader.readLine())!=null){String[] temp = null;temp = lineTXT.split(" ");int count = 0;// 计数器pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);conn.setAutoCommit(false);// 设置数据手动提交,自己管理事务pstmt.setString(1, temp[0]);pstmt.setString(2, temp[0]);pstmt.addBatch();// 用PreparedStatement的批量处理if (count % 2000 == 0) {// 当增加了500个批处理的时候再提交pstmt.executeBatch();// 执行批处理}}conn.commit();pstmt.close();conn.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public void show(){System.out.println("This is string:");for (int i = 0; i
mysql 表 内存溢出_java读取文本,插到mysql表中,出现问题。内存溢出。
最新推荐文章于 2021-02-06 21:23:07 发布
该博客展示了如何使用Java编程语言连接到MySQL数据库,读取文件内容,并执行批量插入操作。通过BufferedReader和PreparedStatement实现文件内容的读取和数据库的交互,涉及数据库连接池、事务管理和批处理。
摘要由CSDN通过智能技术生成