Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. package repository;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import model.Flight;
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12.  
  13. @Configuration
  14. public class FlightRepo
  15. {
  16. private List<Flight> flights = new ArrayList<Flight>();
  17. private Statement statement;
  18.  
  19.  
  20. public FlightRepo() throws SQLException {
  21.  
  22. String url = "jdbc:mysql://127.0.0.1:8080/flights";
  23. String user = "root";
  24. String password = "";
  25. Connection connection;
  26. try
  27. {
  28. Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  29. connection = DriverManager.getConnection(url, user, password);
  30.  
  31. Statement statement = connection.createStatement();
  32. statement.execute("CREATE DATABASE IF NOT EXISTS flights");
  33. statement.execute("USE flights");
  34. System.out.println(connection.getCatalog());
  35. }
  36. catch (Exception e)
  37. {
  38. e.printStackTrace();
  39. }
  40. String query = "select * from flight";
  41. ResultSet result = statement.executeQuery(query);
  42. while(result.next())
  43. {
  44. Integer id = result.getInt("Id");
  45. String dest = result.getString("Destination");
  46. String airp = result.getString("Airport");
  47. Integer frees = result.getInt("FreeSeats");
  48. Date dateh = result.getDate("Date");
  49. Flight flight = new Flight(id,dest,airp,frees,dateh);
  50.  
  51. flights.add(flight);
  52. }
  53. System.out.println("Done2");
  54. }
  55. @Bean
  56. public List<Flight> getAll()
  57. {
  58. return flights;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement