Advertisement
Guest User

Untitled

a guest
Jan 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. package a1;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.io.FileWriter;
  6. import java.sql.Connection;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. /**
  13. * Statistik zur Anzahl der Reservierungen pro Film gespeichert werden.
  14. *
  15. * @author
  16. */
  17. public class a1 {
  18.  
  19. /**
  20. * The main method.
  21. *
  22. * @param _42
  23. * the arguments
  24. * @throws SQLException
  25. * the SQL exception
  26. */
  27. public static void main(String... _42) throws SQLException {
  28.  
  29. Connection con = connectToMyData();
  30. getDatafromDB(con);
  31.  
  32. }
  33.  
  34. /**
  35. * Gets the data from DB.
  36. *
  37. * @param con the connection
  38. * @return the data from DB
  39. * @throws SQLException the SQL exception
  40. */
  41. private static void getDatafromDB(Connection con) throws SQLException {
  42.  
  43. Statement st = null;
  44. System.out.println("Creating statement...");
  45.  
  46. st = con.createStatement();
  47.  
  48.  
  49. // Statistik zur Anzahl der Reservierungen pro Film gespeichert werden.
  50. String SQLANWEISUNG = "SELECT id,name... FROM BLA"; // Input SQL STRING HERE
  51.  
  52. ResultSet rs = st.executeQuery(SQLANWEISUNG);
  53.  
  54.  
  55. while(rs.next()){
  56.  
  57. int id = rs.getInt("id");
  58. int name = rs.getInt("name");
  59. // String first = rs.getString("first");
  60. // String last = rs.getString("last");
  61.  
  62. System.out.println("ID" +id);
  63. System.out.println("Name" +name);
  64.  
  65.  
  66.  
  67.  
  68.  
  69. }
  70.  
  71. //rs.close();
  72. //con.close();
  73. //st.close();
  74.  
  75. }
  76.  
  77. /**
  78. * Write to CSV.
  79. *
  80. * @param b
  81. * @param a
  82. */
  83. private static void writeToCSV(int id, String name) {
  84. // Delimiter used in CSV file
  85.  
  86. final String DELIMITER = ",";
  87. final String NEW_LINE_SEPARATOR = "\n";
  88. final String HEADER = "1,2,3,4,5"; // So viel wie Eintraege
  89. final String FILENAME = "daten.csv";
  90.  
  91. // Liste erstellen
  92. List<Film> f = new ArrayList<>();
  93.  
  94. Film film1 = new Film(id, "name");
  95. Film film2 = new Film(id++, "Name2");
  96. Film film3 = new Film(id += 2, "Name3");
  97.  
  98. f.add(film1);
  99. f.add(film2);
  100. f.add(film3);
  101.  
  102. // Erzeuge FileWriter
  103. FileWriter fileWriter = null;
  104.  
  105. try {
  106. fileWriter = new FileWriter(FILENAME);
  107.  
  108. // Header schreiben
  109. fileWriter.append(HEADER.toString());
  110.  
  111. // New Line
  112. fileWriter.append(NEW_LINE_SEPARATOR);
  113.  
  114. for (Film allElements : f) {
  115. fileWriter.append(String.valueOf(allElements.getId()));
  116. fileWriter.append(DELIMITER);
  117. fileWriter.append(allElements.getName());
  118. fileWriter.append(DELIMITER);
  119. fileWriter.append(NEW_LINE_SEPARATOR);
  120.  
  121. }
  122.  
  123. fileWriter.flush();
  124. fileWriter.close();
  125.  
  126. } catch (Exception e) {
  127. System.err.println("Something went wrong!");
  128. }
  129.  
  130. }
  131.  
  132. /**
  133. * Connect to my data.
  134. *
  135. * @return the connection
  136. */
  137. private static Connection connectToMyData() {
  138. System.out.println("-------- PostgreSQL " + "JDBC Connection Testing ------------");
  139.  
  140. try {
  141.  
  142. Class.forName("org.postgresql.Driver");
  143.  
  144. } catch (ClassNotFoundException e) {
  145.  
  146. System.out.println("Wo ist der PostgreSQL JDBC Treiber? " + "Er muss noch hinzugefuegt werden");
  147. e.printStackTrace();
  148.  
  149. }
  150.  
  151. System.out.println("PostgreSQL JDBC Driver Registered!");
  152.  
  153. Connection connection = null;
  154.  
  155. try {
  156.  
  157. connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/testdb", "name", "123456");
  158.  
  159. } catch (SQLException e) {
  160.  
  161. System.out.println("Connection Failed! Check output console");
  162. e.printStackTrace();
  163.  
  164. }
  165.  
  166. if (connection != null) {
  167. System.out.println("Worked!");
  168. return connection;
  169. } else {
  170. System.out.println("Failed to make connection!");
  171. }
  172. return connection;
  173.  
  174. }
  175.  
  176. // Statistik zur Anzahl der Reservierungen pro Film gespeichert werden.
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement