Guest User

Untitled

a guest
Mar 5th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. package dlw.dataimport;
  2.  
  3. import java.io.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.SQLException;
  8. import java.util.*;
  9.  
  10. import au.com.bytecode.opencsv.*;
  11.  
  12. public class CSVImporter {
  13.  
  14. int[] columns;
  15. Map<Integer, IFilter> filters;
  16. String tableName;
  17.  
  18. public CSVImporter(int[] columns, String tableName) {
  19. this.filters = new HashMap<Integer, IFilter>();
  20. this.columns = columns;
  21. this.tableName = tableName;
  22. }
  23.  
  24. public void attachFilter(int colNumber, IFilter filter) {
  25. filters.put(colNumber, filter);
  26. }
  27.  
  28. protected String buildStatement() {
  29. String rv = "INSERT INTO " + tableName + " VALUES (";
  30. rv += "?";
  31. for (int i = 1; i < columns.length; i++)
  32. rv += ", ?";
  33. rv += ")";
  34. return rv;
  35. }
  36.  
  37. protected String processFilter(int colNumber, String item) {
  38. if (filters.containsKey(colNumber))
  39. return filters.get(colNumber).filter(item);
  40. else
  41. return item;
  42. }
  43.  
  44. public void importData(String inputFile, char separator, String encoding,
  45. Connection connection) throws SQLException, IOException {
  46.  
  47. List<String[]> data = importFile(inputFile, separator, encoding);
  48.  
  49. connection.createStatement().execute("delete from " + tableName);
  50.  
  51. PreparedStatement ps = connection.prepareStatement(buildStatement());
  52.  
  53. int COMMIT_EVERY = 100;
  54.  
  55. // skip the first row, it's the header
  56. for (int i = 1; i < data.size(); i++) {
  57. String[] row = data.get(i);
  58.  
  59. for (int fi = 0; fi < columns.length; fi++) {
  60. int actualCol = columns[fi];
  61. ps.setString(fi + 1, processFilter(actualCol, row[actualCol]));
  62. }
  63.  
  64. ps.addBatch();
  65.  
  66. if (i % COMMIT_EVERY == 0) {
  67. ps.executeBatch();
  68. ps.clearBatch();
  69. }
  70. }
  71.  
  72. ps.executeBatch();
  73. ps.close();
  74.  
  75. }
  76.  
  77. @SuppressWarnings("unchecked")
  78. public static List<String[]> importFile(String filename, char separator,
  79. String encoding) throws IOException {
  80.  
  81. Reader csvfile = new InputStreamReader(new FileInputStream(filename),
  82. encoding);
  83. List<String[]> rv = (List<String[]>) new CSVReader(csvfile, separator)
  84. .readAll();
  85.  
  86. for (String[] a : rv) {
  87. for (int i = 0; i < a.length; i++)
  88. System.out.print("<" + a[i] + ">");
  89. System.out.println();
  90. }
  91. return rv;
  92. }
  93.  
  94. public static void main(String[] args) throws Exception {
  95. Class.forName("com.mysql.jdbc.Driver");
  96.  
  97. Connection conn = DriverManager.getConnection(
  98. "jdbc:mysql://localhost:8889/dlworkshop", "root", "root");
  99.  
  100. CSVImporter ccpayment = new CSVImporter(
  101. new int[] { 2, 3, 4, 5, 6, 10 }, "card_payment");
  102. ccpayment.importData(
  103. "/Users/Domenico/Desktop/dlworkshop/pagamenti.csv", '\t',
  104. "UTF8", conn);
  105. }
  106. }
Add Comment
Please, Sign In to add comment