java怎么實現(xiàn)批量導入csv文件到mysql數(shù)據(jù)庫-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“java怎么實現(xiàn)批量導入csv文件到mysql數(shù)據(jù)庫”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司主要從事網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務桂林,十多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:18980820575
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("數(shù)據(jù)庫連接成功"); insert_data(in);  long EndTime = System.currentTimeMillis(); long time = (EndTime - startTime) / 1000;  System.out.println("導入數(shù)據(jù)共用時:" + 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(); } } }

“java怎么實現(xiàn)批量導入csv文件到mysql數(shù)據(jù)庫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

新聞名稱:java怎么實現(xiàn)批量導入csv文件到mysql數(shù)據(jù)庫-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://muchs.cn/article16/pcodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務器、做網(wǎng)站自適應網(wǎng)站、網(wǎng)站營銷、軟件開發(fā)、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計