Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a1;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.io.FileWriter;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Statistik zur Anzahl der Reservierungen pro Film gespeichert werden.
- *
- * @author
- */
- public class a1 {
- /**
- * The main method.
- *
- * @param _42
- * the arguments
- * @throws SQLException
- * the SQL exception
- */
- public static void main(String... _42) throws SQLException {
- Connection con = connectToMyData();
- getDatafromDB(con);
- }
- /**
- * Gets the data from DB.
- *
- * @param con the connection
- * @return the data from DB
- * @throws SQLException the SQL exception
- */
- private static void getDatafromDB(Connection con) throws SQLException {
- Statement st = null;
- System.out.println("Creating statement...");
- st = con.createStatement();
- // Statistik zur Anzahl der Reservierungen pro Film gespeichert werden.
- String SQLANWEISUNG = "SELECT id,name... FROM BLA"; // Input SQL STRING HERE
- ResultSet rs = st.executeQuery(SQLANWEISUNG);
- while(rs.next()){
- int id = rs.getInt("id");
- int name = rs.getInt("name");
- // String first = rs.getString("first");
- // String last = rs.getString("last");
- System.out.println("ID" +id);
- System.out.println("Name" +name);
- }
- //rs.close();
- //con.close();
- //st.close();
- }
- /**
- * Write to CSV.
- *
- * @param b
- * @param a
- */
- private static void writeToCSV(int id, String name) {
- // Delimiter used in CSV file
- final String DELIMITER = ",";
- final String NEW_LINE_SEPARATOR = "\n";
- final String HEADER = "1,2,3,4,5"; // So viel wie Eintraege
- final String FILENAME = "daten.csv";
- // Liste erstellen
- List<Film> f = new ArrayList<>();
- Film film1 = new Film(id, "name");
- Film film2 = new Film(id++, "Name2");
- Film film3 = new Film(id += 2, "Name3");
- f.add(film1);
- f.add(film2);
- f.add(film3);
- // Erzeuge FileWriter
- FileWriter fileWriter = null;
- try {
- fileWriter = new FileWriter(FILENAME);
- // Header schreiben
- fileWriter.append(HEADER.toString());
- // New Line
- fileWriter.append(NEW_LINE_SEPARATOR);
- for (Film allElements : f) {
- fileWriter.append(String.valueOf(allElements.getId()));
- fileWriter.append(DELIMITER);
- fileWriter.append(allElements.getName());
- fileWriter.append(DELIMITER);
- fileWriter.append(NEW_LINE_SEPARATOR);
- }
- fileWriter.flush();
- fileWriter.close();
- } catch (Exception e) {
- System.err.println("Something went wrong!");
- }
- }
- /**
- * Connect to my data.
- *
- * @return the connection
- */
- private static Connection connectToMyData() {
- System.out.println("-------- PostgreSQL " + "JDBC Connection Testing ------------");
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Wo ist der PostgreSQL JDBC Treiber? " + "Er muss noch hinzugefuegt werden");
- e.printStackTrace();
- }
- System.out.println("PostgreSQL JDBC Driver Registered!");
- Connection connection = null;
- try {
- connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/testdb", "name", "123456");
- } catch (SQLException e) {
- System.out.println("Connection Failed! Check output console");
- e.printStackTrace();
- }
- if (connection != null) {
- System.out.println("Worked!");
- return connection;
- } else {
- System.out.println("Failed to make connection!");
- }
- return connection;
- }
- // Statistik zur Anzahl der Reservierungen pro Film gespeichert werden.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement