Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. package com.savecsvtopostgres;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. public class SaveCsvDataToPostgres {
  14.  
  15. List<OauthResponse> responseList = new ArrayList<OauthResponse>();
  16.  
  17. public static void main(String[] args){
  18.  
  19. BufferedReader csvBuffer = null;
  20. List<OauthResponse> csvlist = new ArrayList<OauthResponse>();
  21.  
  22. try {
  23. String csvLine;
  24. csvBuffer = new BufferedReader(new FileReader("C:\Users\DVSabareesh\testresults.csv"));
  25.  
  26. while ((csvLine = csvBuffer.readLine()) != null) {
  27. System.out.println("Raw CSV data: " + csvLine);
  28. OauthResponse oauthResponse = csvtoArrayList(csvLine);
  29. csvlist.add(oauthResponse);
  30. }
  31. insert(csvlist);
  32. System.out.println("Converted ArrayList data: " + csvlist.size() + "n");
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. } finally {
  38. try {
  39. if (csvBuffer != null) csvBuffer.close();
  40. } catch (IOException exception) {
  41. exception.printStackTrace();
  42. }
  43. }
  44. }
  45.  
  46. // Utility which converts CSV to ArrayList using Split Operation
  47. public static OauthResponse csvtoArrayList(String csvLine) {
  48. OauthResponse oauthResponse = new OauthResponse();
  49.  
  50. if (csvLine != null) {
  51. String[] splitData = csvLine.split(",");
  52. int datalength = splitData.length;
  53. System.out.println(datalength);
  54.  
  55. oauthResponse.setId(splitData[0]);
  56. oauthResponse.setValue2(splitData[1]);
  57. oauthResponse.setTicketName(splitData[2]);
  58. oauthResponse.setTicketResponse(splitData[3]);
  59. oauthResponse.setResponseId(splitData[4]);
  60. oauthResponse.setResponseMessage(splitData[5]);
  61. oauthResponse.setOperations(splitData[6]);
  62. oauthResponse.setFileType(splitData[7]);
  63. oauthResponse.setBooleanValue(splitData[8]);
  64. oauthResponse.setId2(splitData[9]);
  65. oauthResponse.setId3(splitData[10]);
  66. oauthResponse.setOauthUrl(splitData[11]);
  67. if(datalength == 13){
  68. oauthResponse.setValue13(splitData[12]);
  69. }else{
  70. oauthResponse.setValue13("");
  71. }
  72. }
  73. return oauthResponse;
  74. }
  75.  
  76. // Utility which used to insert data into postgres
  77. public static void insert(List<OauthResponse> responseList) throws SQLException {
  78. Connection dbConnection = null;
  79. PreparedStatement ps = null;
  80. try {
  81. dbConnection = getDBConnection();
  82. dbConnection.setAutoCommit(false);
  83. String query = "INSERT INTO csvvalues("
  84. + "testgeneratorid, "
  85. + "ticketvalues1, "
  86. + "ticketname, "
  87. + "ticketresponsecode, "
  88. + "ticketresponsemessage, "
  89. + "testcaseresults, "
  90. + "resultformat, "
  91. + "booleanresult, "
  92. + "ticketvalues2, "
  93. + "ticketvalues3, "
  94. + "oauthurl, "
  95. + "ticketvalues4, "
  96. + "ticketvalues5) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  97. ps = dbConnection.prepareStatement(query);
  98.  
  99. System.out.println("Records are getting inserted into TestSuiteResults.TestCase table!");
  100.  
  101. for(OauthResponse res : responseList){
  102. ps.setString(1,res.getId());
  103. ps.setString(2,res.getValue2());
  104. ps.setString(3,res.getTicketName());
  105. ps.setString(4,res.getTicketResponse());
  106. ps.setString(5,res.getResponseId());
  107. ps.setString(6,res.getResponseMessage());
  108. ps.setString(7,res.getOperations());
  109. ps.setString(8,res.getFileType());
  110. ps.setString(9,res.getBooleanValue());
  111. ps.setString(10,res.getId2());
  112. ps.setString(11,res.getId3());
  113. ps.setString(12,res.getOauthUrl());
  114. ps.setString(13,res.getValue13());
  115. ps.addBatch();
  116. }
  117. ps.executeBatch();
  118. dbConnection.commit();
  119. System.out.println("Record is inserted into DBUSER table!");
  120. } catch (SQLException e) {
  121. e.getNextException().printStackTrace();
  122. dbConnection.rollback();
  123. } finally {
  124. if (ps != null) {
  125. ps.close();
  126. }
  127. if (dbConnection != null) {
  128. dbConnection.close();
  129. }
  130. }
  131. }
  132.  
  133. // Utility which gets connection to postgres
  134. public static Connection getDBConnection() {
  135. Connection dbConnection = null;
  136. try {
  137. //Class.forName("org.postgresql.Driver");
  138. //dbConnection = DriverManager.getConnection(
  139. //"jdbc:postgresql://localhost:5432/Database4TesSuitetResults", "postgres", "santhosh");
  140. Class.forName("com.mysql.jdbc.Driver");
  141. dbConnection = DriverManager.getConnection(
  142. "jdbc:mysql://localhost:3309/csv","root","system");
  143.  
  144. System.out.println("DB connected");
  145. return dbConnection;
  146. } catch (SQLException | ClassNotFoundException e) {
  147. System.out.println(e.getMessage());
  148. }
  149. return dbConnection;
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement