示例代碼如下:
import java.io.*; import java.sql.*; import java.util.Objects; class DataReader { File dataSource=new File("/home/kisetsu/Documents/test_nsrxx"); void readData() { //聲明Connection對象 Connection con; //驅動程序名 String driver = "com.mysql.jdbc.Driver"; //URL指向要訪問的數據庫名mydata String url = "jdbc:mysql://localhost:3306/invoice"; //MySQL配置時的用戶名 String user = "root"; //MySQL配置時的密碼 String password = "5702"; //遍歷查詢結果集 FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; //用於包裝InputStreamReader,提高處理性能。因為BufferedReader有緩沖的,而InputStreamReader沒有。 try { Class.forName(driver); //getConnection()方法,連接MySQL數據庫 con = DriverManager.getConnection(url, user, password); if (!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); //創建statement類對象,用來執行SQL語句 Statement statement = con.createStatement(); String str = ""; String str1 = ""; fis = new FileInputStream("/home/kisetsu/Documents/test_fpxx");// FileInputStream // 從文件系統中的某個文件中獲取字節 isr = new InputStreamReader(fis);// InputStreamReader 是字節流通向字符流的橋梁, br = new BufferedReader(isr);// 從字符輸入流中讀取文件中的內容,封裝了一個new InputStreamReader的對象 int count=0; double errorCount=0; long start=System.currentTimeMillis(); //624087 while ((str = br.readLine()) != null) { String[] infomation=str.split(","); count++; if(count<274850){ continue; } if(count%100==0){ System.out.println(count/100+""); System.out.println(errorCount/(double) count+""); } // System.out.println(""); for (int i=0;i<infomation.length;i++) { if(Objects.equals(infomation[i], "null")){ infomation[i]="-1"; } } PreparedStatement psql; ResultSet res; psql = con.prepareStatement("insert into fpxx " + "(xh,fp_nid,gfxbh,xfsbh,je,se,ze,kpyf,kprq,zfbz) " + "values(?,?,?,?,?,?,?,?,?,?)"); // try { int len=infomation.length; // for (String s:infomation) { // System.out.println(s); // } psql.setInt(1,count); psql.setString(2,infomation[0].substring(1)); psql.setString(3,infomation[1]); psql.setString(4,infomation[2]); // psql.setString(5,infomation[3]); // psql.setString(6,infomation[4]); // psql.setDouble(4,Double.parseDouble(infomation[infomation.length-5])); // psql.setDouble(5,Double.parseDouble(infomation[infomation.length-4])); psql.setDouble(5,Double.parseDouble(infomation[3])); psql.setDouble(6,Double.parseDouble(infomation[4])); psql.setDouble(7,Double.parseDouble(infomation[5])); psql.setInt(8,Integer.parseInt(infomation[6])); psql.setString(9,infomation[7]); psql.setString(10,infomation[8].charAt(0)+""); psql.executeUpdate(); //執行更新 // }catch (Exception e){ // errorCount++; // System.out.println("第"+count+"條數據出現錯誤"); // System.out.println("-------------------"); // System.out.println(count+" ---- "+str); // } // if(count>500){ // break; // } } long end=System.currentTimeMillis(); System.out.println(end-start); con.close(); // 當讀取的一行不為空時,把讀到的str的值賦給str1// 打印出str1 } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); } catch (IOException e) { System.out.println("讀取文件失敗"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { try { br.close(); isr.close(); fis.close(); // 關閉的時候最好按照先后順序關閉最后開的先關閉所以先關s,再關n,最后關m } catch (IOException | NullPointerException e) { e.printStackTrace(); } } } }