Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. package rpa;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.util.zip.ZipEntry;
  12. import java.util.zip.ZipInputStream;
  13.  
  14. import com.mysql.jdbc.Statement;
  15.  
  16. public class DataBase {
  17. public static void main(String[] args) {
  18. unzip();
  19. BaseDados();
  20.  
  21. }
  22.  
  23. public static void BaseDados() {
  24. // Directory path here
  25. String path = "C:\\Users\\WNDWS\\Downloads\\NosVodafone";
  26.  
  27. String files;
  28. File folder = new File(path);
  29. File[] listOfFiles = folder.listFiles();
  30.  
  31. for (int i = 0; i < listOfFiles.length; i++) {
  32.  
  33. if (listOfFiles[i].isFile()) {
  34. files = listOfFiles[i].getName();
  35.  
  36. if (files.contains("DownloadCD")) {// || files.contains("DownloadEncargosCorrentes")) { // vodafone
  37.  
  38. LerCSV(files, "vodafone");
  39.  
  40. } else { // nos
  41. LerCSV(files, "nos");
  42. }
  43.  
  44. }
  45. }
  46.  
  47. }
  48.  
  49. public static void LerCSV(String csvFile, String rede) {
  50.  
  51. String path = "C:\\Users\\WNDWS\\Downloads\\NosVodafone\\";
  52. BufferedReader br = null;
  53. String line = "";
  54. String cvsSplitBy = ";";
  55.  
  56. try {
  57.  
  58. String charset = "ISO-8859-1"; // or what corresponds
  59. br = new BufferedReader(new InputStreamReader(new FileInputStream(path + csvFile), charset));
  60.  
  61. // br = new BufferedReader(new FileReader(path + csvFile));
  62. boolean flag = false;
  63. while ((line = br.readLine()) != null) {
  64.  
  65. // use comma as separator
  66. String[] parametros = line.split(cvsSplitBy);
  67.  
  68. if (parametros[0].charAt(1) == 'C' && flag == true && rede.equals("nos")) {
  69. for (int i = 0; i < parametros.length; i++) {
  70. parametros[i] = parametros[i].replace("\"", "");
  71. }
  72.  
  73. CreateDataNos(parametros);
  74.  
  75. } else if (parametros[0].charAt(1) == 'C' && rede.equals("nos"))
  76. flag = true;
  77. else if (parametros[0].charAt(0) == 'C' && rede.equals("vodafone"))
  78. flag = true;
  79. else if (rede.equals("vodafone") && flag == true) {
  80. CreateDataVodafone(parametros);
  81.  
  82. }
  83.  
  84. }
  85.  
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89.  
  90. }
  91.  
  92. public static void CreateDataVodafone(String[] parametros) {
  93. final String USERDATA = "root";
  94. final String PASSDATA = "wndws";
  95. final String CONNECTION = "jdbc:mysql://localhost:3306/vodafone?useSSL=false";
  96.  
  97. try {
  98. Connection myConnection = DriverManager.getConnection(CONNECTION, USERDATA, PASSDATA);
  99.  
  100. Statement stmt = (Statement) myConnection.createStatement();
  101.  
  102. String sql = "INSERT IGNORE INTO comunicacoes (numero, conta,tipo, destino, descricao, durCham, durCum, custo) VALUES "
  103. + "('" + parametros[1] + "','" + parametros[0] + "','" + parametros[2] + "','" + parametros[6]
  104. + parametros[7] + parametros[8] + "','" + parametros[9] + "','" + parametros[10] + "','"
  105. + parametros[11] + "','" + parametros[12] + "')";
  106.  
  107. stmt.executeUpdate(sql);
  108. myConnection.close();
  109. } catch (Exception e) {
  110. // TODO: handle exception
  111. e.printStackTrace();
  112. }
  113.  
  114. }
  115.  
  116. public static void CreateDataNos(String[] parametros) {
  117. final String USERDATA = "root";
  118. final String PASSDATA = "wndws";
  119. final String CONNECTION = "jdbc:mysql://localhost:3306/nos?useSSL=false";
  120.  
  121. try {
  122. Connection myConnection = DriverManager.getConnection(CONNECTION, USERDATA, PASSDATA);
  123.  
  124. Statement stmt = (Statement) myConnection.createStatement();
  125.  
  126. String sql = "INSERT IGNORE INTO registos (numero, conta, tarifario) VALUES " + "('" + parametros[3] + "','"
  127. + parametros[2] + "','" + parametros[4] + "')";
  128.  
  129. stmt.executeUpdate(sql);
  130.  
  131. sql = "INSERT IGNORE INTO contactos (origem, destino, descricao, data, hora, quantidade, unidade, valor) VALUES "
  132. + "('" + parametros[9] + "','" + parametros[10] + "','" + parametros[5] + "','" + parametros[7]
  133. + "','" + parametros[8] + "','" + parametros[11] + "','" + parametros[12] + "','" + parametros[13]
  134. + "')";
  135. ;
  136. stmt.executeUpdate(sql);
  137. myConnection.close();
  138.  
  139. // ResultSet myResultSet = stmt.executeQuery("select * from registos");
  140. // while (myResultSet.next())
  141. // System.out.println(myResultSet.getString("numero"));
  142.  
  143. /*
  144. *
  145. * String sql = "CREATE TABLE REGISTOS " + " conta VARCHAR(255), " +
  146. * " numero INTEGER, " + " tarifario VARCHAR(255), " +
  147. * " PRIMARY KEY ( numero ))"; stmt.executeUpdate(sql);
  148. */
  149. /*
  150. * Statement myStatement = (Statement) myConnection.createStatement(); ResultSet
  151. * myResultSet = myStatement.executeQuery("select * from city"); while
  152. * (myResultSet.next()) System.out.println(myResultSet.getString("name"));
  153. */
  154. } catch (Exception e) {
  155. // TODO: handle exception
  156. e.printStackTrace();
  157. }
  158. }
  159.  
  160. private static void unzip() {
  161. // Directory path here
  162. String path = "C:\\Users\\WNDWS\\Downloads\\";
  163.  
  164. String files;
  165. File folder = new File(path);
  166. File[] listOfFiles = folder.listFiles();
  167.  
  168. String destDir = path + "NosVodafone";
  169. for (int i = 0; i < listOfFiles.length; i++) {
  170.  
  171. if (listOfFiles[i].isFile()) {
  172. files = listOfFiles[i].getName();
  173. if (files.endsWith(".zip")) {
  174. // System.out.println(files);
  175. if (files.contains("_ed_") && files.contains("_ED")) { // nos
  176. String zipFilePath = path + files;
  177.  
  178. unzipFile(zipFilePath, destDir);
  179. deleteFile(zipFilePath);
  180. String csvName = files.replaceAll("zip", "csv");
  181. LerCSV(csvName, "nos");
  182. } else if (files.contains("DownloadCD") || files.contains("DownloadEncargosCorrentes")) { // vodafone
  183. String zipFilePath = path + files;
  184.  
  185. unzipFile(zipFilePath, destDir);
  186. deleteFile(zipFilePath);
  187. }
  188.  
  189. }
  190. }
  191. }
  192. }
  193.  
  194. private static void unzipFile(String zipFilePath, String destDir) {
  195. File dir = new File(destDir);
  196. // Se dir nao existir criar
  197. if (!dir.exists())
  198. dir.mkdirs();
  199. FileInputStream fis;
  200. // buffer ler e escrever
  201. byte[] buffer = new byte[1024];
  202. try {
  203. fis = new FileInputStream(zipFilePath);
  204. ZipInputStream zis = new ZipInputStream(fis);
  205. ZipEntry ze = zis.getNextEntry();
  206. while (ze != null) {
  207. String fileName = ze.getName();
  208. File newFile = new File(destDir + File.separator + fileName);
  209. // System.out.println("Unzipping to " + newFile.getAbsolutePath());
  210. // create directories for sub directories in zip
  211. new File(newFile.getParent()).mkdirs();
  212. FileOutputStream fos = new FileOutputStream(newFile);
  213. int len;
  214. while ((len = zis.read(buffer)) > 0) {
  215. fos.write(buffer, 0, len);
  216. }
  217. fos.close();
  218. // fechar ZipEntry
  219. zis.closeEntry();
  220. ze = zis.getNextEntry();
  221. }
  222. // fechar ultimo ZipEntry
  223. zis.closeEntry();
  224. zis.close();
  225. fis.close();
  226. } catch (IOException e) {
  227. e.printStackTrace();
  228. }
  229.  
  230. }
  231.  
  232. public static void deleteFile(String path) {
  233. try {
  234.  
  235. File file = new File(path);
  236.  
  237. if (file.delete()) {
  238. System.out.println(file.getName() + " is deleted!");
  239. } else {
  240. System.out.println("Delete operation is failed.");
  241. }
  242.  
  243. } catch (Exception e) {
  244.  
  245. e.printStackTrace();
  246.  
  247. }
  248.  
  249. }
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement