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
java读取文本,插到mysql表中,出现问题。内存溢出。
标签:mysql java
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:http://jinkuake.blog.51cto.com/8257504/1748841