public static List<String[]> readRecords(InputStream csvFile) throws FileNotFoundException, IOException, InvalidNumberOfElementsException { BufferedReader reader = new BufferedReader(new InputStreamReader(csvFile, StandardCharsets.UTF_8)); List<String[]> data = new ArrayList<String[]>(); int item_count = -1; int line_number = 0; while (true) { String line = reader.readLine(); line_number++; if (line == null) { break; } String[] items = getStringItems(line); if (item_count == -1) { item_count = items.length; } else if (item_count != items.length) { throw new InvalidNumberOfElementsException( String.format(INVALID_NUMBER_D_OF_ELEMENTS_AT_LINE_D_INITIAL_COLUMNS_NUMBER_D, items.length, line_number, item_count)); } data.add(items); } reader.close(); return data; }
public void insert() throws Exception { Connection connection = null; try { connection = getConnection(); List<String[]> records = TableDataReader.readRecords(new ByteArrayInputStream(content)); insertRecords(connection, records, tableName); } finally { closeConnection(connection); } }
private void deleteRowsDataFromTable(String tableName, String primaryKey, byte[] fileContent) throws Exception { Connection connection = null; try { connection = dataSource.getConnection(); List<String[]> records = TableDataReader.readRecords(new ByteArrayInputStream(fileContent)); for (String[] record : records) { if (record.length > 0) { String sql = SqlFactory.getNative(connection).delete().from(tableName).where(primaryKey + " = ?").build(); PreparedStatement deleteStatement = connection.prepareStatement(sql); deleteStatement.setObject(1, record[0]); deleteStatement.execute(); } else { logger.error(String.format("Skipping deletion of an empty data row for table: %s", tableName)); } } } finally { if (connection != null) { connection.close(); } } }
try { connection = dataSource.getConnection(); List<String[]> records = TableDataReader.readRecords(new ByteArrayInputStream(fileContent)); for (String[] record : records) { if (record.length > 0) {