Search in sources :

Example 1 with Booking

use of org.apache.karaf.examples.jdbc.api.Booking in project karaf by apache.

the class AddCommand method execute.

@Override
public Object execute() throws Exception {
    Booking booking = new Booking();
    if (id == 0) {
        Random random = new Random();
        id = Long.valueOf(random.nextInt(9000000) + 1000000);
    }
    booking.setId(id);
    booking.setCustomer(customer);
    booking.setFlight(flight);
    bookingService.add(booking);
    return null;
}
Also used : Random(java.util.Random) Booking(org.apache.karaf.examples.jdbc.api.Booking)

Example 2 with Booking

use of org.apache.karaf.examples.jdbc.api.Booking in project karaf by apache.

the class ListCommand method execute.

@Override
public Object execute() throws Exception {
    ShellTable table = new ShellTable();
    table.column("ID");
    table.column("Flight");
    table.column("Customer");
    for (Booking booking : bookingService.list()) {
        table.addRow().addContent(booking.getId(), booking.getFlight(), booking.getCustomer());
    }
    table.print(System.out);
    return null;
}
Also used : ShellTable(org.apache.karaf.shell.support.table.ShellTable) Booking(org.apache.karaf.examples.jdbc.api.Booking)

Example 3 with Booking

use of org.apache.karaf.examples.jdbc.api.Booking in project karaf by apache.

the class BookingServiceJdbcImpl method list.

@Override
public Collection<Booking> list() {
    Collection<Booking> bookings = new ArrayList<>();
    try (Connection connection = dataSource.getConnection()) {
        try (PreparedStatement selectStatement = connection.prepareStatement(selectBookingSql)) {
            ResultSet rs = selectStatement.executeQuery();
            while (rs.next()) {
                Booking booking = new Booking();
                booking.setId(rs.getLong("id"));
                booking.setCustomer(rs.getString("customer"));
                booking.setFlight(rs.getString("flight"));
                bookings.add(booking);
            }
        } catch (SQLException exception) {
            LOGGER.error("Can't retreive the bookings", exception);
        }
    } catch (Exception exception) {
        LOGGER.error("Error getting connection ", exception);
    }
    return bookings;
}
Also used : Booking(org.apache.karaf.examples.jdbc.api.Booking)

Example 4 with Booking

use of org.apache.karaf.examples.jdbc.api.Booking in project karaf by apache.

the class GetCommand method execute.

@Override
public Object execute() throws Exception {
    ShellTable table = new ShellTable();
    table.column("ID");
    table.column("Flight");
    table.column("Customer");
    Booking booking = bookingService.get(id);
    table.addRow().addContent(booking.getId(), booking.getFlight(), booking.getCustomer());
    table.print(System.out);
    return null;
}
Also used : ShellTable(org.apache.karaf.shell.support.table.ShellTable) Booking(org.apache.karaf.examples.jdbc.api.Booking)

Example 5 with Booking

use of org.apache.karaf.examples.jdbc.api.Booking in project karaf by apache.

the class BookingServiceJdbcImpl method get.

@Override
public Booking get(Long id) {
    try (Connection connection = dataSource.getConnection()) {
        String sqlQuery = selectBookingSql + " where id = ?";
        try (PreparedStatement selectStatement = connection.prepareStatement(sqlQuery)) {
            selectStatement.setLong(1, id);
            ResultSet rs = selectStatement.executeQuery();
            if (rs.next()) {
                Booking booking = new Booking();
                booking.setId(id);
                booking.setCustomer(rs.getString("customer"));
                booking.setFlight(rs.getString("flight"));
                return booking;
            }
        } catch (SQLException exception) {
            LOGGER.error("Can't find booking with id {}", id, exception);
        }
    } catch (Exception exception) {
        LOGGER.error("Error getting connection ", exception);
    }
    return null;
}
Also used : Booking(org.apache.karaf.examples.jdbc.api.Booking)

Aggregations

Booking (org.apache.karaf.examples.jdbc.api.Booking)5 ShellTable (org.apache.karaf.shell.support.table.ShellTable)2 Random (java.util.Random)1