Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.PrintWriter;
  5.  
  6.  
  7. import java.sql.*;
  8. import java.util.*;
  9.  
  10.  
  11.  
  12.  
  13. public class zone {
  14. List <model> listOfBlogs;
  15.  
  16. public zone()
  17. {
  18.  
  19. Connection conn = null;
  20. listOfBlogs = new ArrayList<model>();
  21.  
  22.  
  23. // connect to the database
  24.  
  25. conn = connectToDatabaseOrDie();
  26.  
  27.  
  28. // get the data
  29. populateListOfTopics(conn, listOfBlogs);
  30.  
  31. //print the data
  32. printTopics(listOfBlogs);
  33.  
  34.  
  35.  
  36. }
  37.  
  38.  
  39. public List<model> printTopics(List<model> listOfBlogs)
  40. {
  41. Iterator<model> it = listOfBlogs.iterator();
  42. while (it.hasNext())
  43. {
  44. model blog = (model)it.next();
  45. System.out.println( blog.day + " " + blog.hour +" " + blog.zone_id);
  46. }
  47. return listOfBlogs;
  48. }
  49.  
  50.  
  51. private void populateListOfTopics(Connection conn, List<model> listOfBlogs)
  52. {
  53. try
  54. {
  55. for(int j=9;j<10;j++){
  56. for(int i=10;i<12;i++){
  57.  
  58. String sql= "SELECT day,hour,zone_id FROM public.zone_block_table where day=? and hour=?";
  59. PreparedStatement pstmt = conn.prepareStatement(sql);
  60. pstmt.setInt(1,j);
  61. pstmt.setInt(2,i);
  62.  
  63.  
  64. ResultSet rs = pstmt.executeQuery();
  65.  
  66.  
  67.  
  68. while ( rs.next() )
  69. {
  70. model blog = new model();
  71.  
  72. blog.day = rs.getInt ("day");
  73. blog.hour=rs.getInt ("hour");
  74. blog.zone_id = rs.getInt ("zone_id");
  75.  
  76.  
  77.  
  78. listOfBlogs.add(blog);
  79.  
  80. }
  81. convertToCsv(rs,i,j);
  82.  
  83. rs.close();
  84. pstmt.close();
  85.  
  86. }
  87. }
  88. }
  89. catch (SQLException se) {
  90. System.err.println("Threw a SQLException creating the list of state.");
  91. System.err.println(se.getMessage());
  92. } catch (Exception e) {
  93. System.out.println("Err");
  94. e.printStackTrace();
  95. }
  96. }
  97.  
  98.  
  99.  
  100.  
  101. private Connection connectToDatabaseOrDie()
  102. {
  103. Connection conn = null;
  104. try
  105. {
  106. Class.forName("org.postgresql.Driver");
  107. String url = "jdbc:postgresql://localhost:5432/mvc_data_base";
  108. conn = DriverManager.getConnection(url,"postgres", "122333");
  109. }
  110. catch (ClassNotFoundException e)
  111. {
  112. e.printStackTrace();
  113.  
  114.  
  115.  
  116. }
  117. catch (SQLException e)
  118. {
  119. e.printStackTrace();
  120.  
  121. }
  122. return conn;
  123. }
  124.  
  125. public void convertToCsv(ResultSet rs,int k,int l) throws SQLException, FileNotFoundException {
  126. PrintWriter csvWriter = new PrintWriter(new File("state_"+k+"_"+l+".csv")) ;
  127. ResultSetMetaData meta = rs.getMetaData() ;
  128. int numberOfColumns = meta.getColumnCount() ;
  129. String dataHeaders = """ + meta.getColumnName(1) + """ ;
  130. for (int i = 2 ; i < numberOfColumns + 1 ; i ++ ) {
  131. dataHeaders += ","" + meta.getColumnName(i) + """ ;
  132. }
  133. csvWriter.println(dataHeaders) ;
  134. while (rs.next()) {
  135. String row = """ + rs.getString(1) + """ ;
  136. for (int i = 2 ; i < numberOfColumns + 1 ; i ++ ) {
  137. row += ","" + rs.getString(i) + """ ;
  138. }
  139. csvWriter.println(row) ;
  140. }
  141. csvWriter.close();
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement