Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package repository;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import model.Flight;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class FlightRepo
- {
- private List<Flight> flights = new ArrayList<Flight>();
- private Statement statement;
- public FlightRepo() throws SQLException {
- String url = "jdbc:mysql://127.0.0.1:8080/flights";
- String user = "root";
- String password = "";
- Connection connection;
- try
- {
- Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
- connection = DriverManager.getConnection(url, user, password);
- Statement statement = connection.createStatement();
- statement.execute("CREATE DATABASE IF NOT EXISTS flights");
- statement.execute("USE flights");
- System.out.println(connection.getCatalog());
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- String query = "select * from flight";
- ResultSet result = statement.executeQuery(query);
- while(result.next())
- {
- Integer id = result.getInt("Id");
- String dest = result.getString("Destination");
- String airp = result.getString("Airport");
- Integer frees = result.getInt("FreeSeats");
- Date dateh = result.getDate("Date");
- Flight flight = new Flight(id,dest,airp,frees,dateh);
- flights.add(flight);
- }
- System.out.println("Done2");
- }
- @Bean
- public List<Flight> getAll()
- {
- return flights;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement