java实现批量导入.csv文件到mysql数据库
程序员文章站
2023-10-30 18:16:04
这篇博文是在参加ccf时导入.csv文件时自己总结的,虽然navicatformysql可以导入.csv文件,可是当我导入的时候不知道是文件太大还是什么原因,总是会出现失败...
这篇博文是在参加ccf时导入.csv文件时自己总结的,虽然navicatformysql可以导入.csv文件,可是当我导入的时候不知道是文件太大还是什么原因,总是会出现失败。然后就用java写了一个批量导入数据的类去导入该.csv文件,这里也没有考虑代码的结构,只是为了快速的完成这个工作,做一个总结。
package com.cqu.price_prediction.farm; import java.io.file; import java.io.filenotfoundexception; import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.sqlexception; import java.util.scanner; public class read { private static connection con; public static void main(string[] args) throws filenotfoundexception, sqlexception { long starttime = system.currenttimemillis(); file file = new file("h:/agriculturalproduct/data/farming.csv"); scanner in = new scanner(file); getconnect(); system.out.println("数据库连接成功"); insert_data(in); long endtime = system.currenttimemillis(); long time = (endtime - starttime) / 1000; system.out.println("导入数据共用时:" + time); } private static void insert_data(scanner in) throws sqlexception { int count = 0; string sql = "insert into farming (province,market,type,name,standard,area,color,unit,minprice,avgprice,maxprice,entertime,time)" + "values(?,?,?,?,?,?,?,?,?,?,?,?,?)"; con.setautocommit(false); preparedstatement pstmt = con.preparestatement(sql); in.next(); while (in.hasnext()) { string temp1 = in.nextline(); string[] temp = temp1.split(","); if (temp.length < 13) continue; if (temp.length == 13) { pstmt.setstring(1, temp[0]); pstmt.setstring(2, temp[1]); pstmt.setstring(3, temp[2]); pstmt.setstring(4, temp[3]); pstmt.setstring(5, temp[4]); pstmt.setstring(6, temp[5]); pstmt.setstring(7, temp[6]); pstmt.setstring(8, temp[7]); pstmt.setstring(9, temp[8]); pstmt.setstring(10, temp[9]); pstmt.setstring(11, temp[10]); pstmt.setstring(12, temp[11]); pstmt.setstring(13, temp[12]); } pstmt.addbatch(); count++; if (count == 20000) { count = execute(pstmt, count); } } pstmt.executebatch(); con.commit(); } public static int execute(preparedstatement pstmt, int count) throws sqlexception { pstmt.executebatch(); con.commit(); return 0; } private static void getconnect() { try { class.forname("com.mysql.jdbc.driver"); con = drivermanager.getconnection( "jdbc:mysql://localhost:3306/agricultural_price_prediction?useunicode=true&characterencoding=utf8&useserverprepstmts=false&rewritebatchedstatements=true", "root", "123456"); } catch (classnotfoundexception | sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。