示例代码如下:
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(); } } } }