NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.*;
import java.time.format.DateTimeFormatter;

public class MainScene extends Application {
private TableView<Flight> flightTable;

@Override
public void start(Stage primaryStage) {
// Создание элементов интерфейса
Label label = new Label("Введите имя базы данных:");
TextField databaseNameField = new TextField();
Button connectButton = new Button("Подключиться");

// Таблица для отображения рейсов
flightTable = new TableView<>();

TableColumn<Flight, String> flightNumberColumn = new TableColumn<>("Номер рейса");
flightNumberColumn.setCellValueFactory(new PropertyValueFactory<>("flightNumber"));

TableColumn<Flight, String> airlineColumn = new TableColumn<>("Авиакомпания");
airlineColumn.setCellValueFactory(new PropertyValueFactory<>("airline"));

TableColumn<Flight, String> departureCityColumn = new TableColumn<>("Город вылета");
departureCityColumn.setCellValueFactory(new PropertyValueFactory<>("departureCity"));

TableColumn<Flight, String> arrivalCityColumn = new TableColumn<>("Город назначения");
arrivalCityColumn.setCellValueFactory(new PropertyValueFactory<>("arrivalCity"));

TableColumn<Flight, String> departureDateColumn = new TableColumn<>("Дата вылета");
departureDateColumn.setCellValueFactory(new PropertyValueFactory<>("departureDate"));

TableColumn<Flight, String> arrivalDateColumn = new TableColumn<>("Дата прибытия");
arrivalDateColumn.setCellValueFactory(new PropertyValueFactory<>("arrivalDate"));

TableColumn<Flight, String> departureTimeColumn = new TableColumn<>("Время вылета");
departureTimeColumn.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().getDepartureTime().format(DateTimeFormatter.ofPattern("HH:mm")))
);

TableColumn<Flight, String> arrivalTimeColumn = new TableColumn<>("Время прибытия");
arrivalTimeColumn.setCellValueFactory(cellData ->
new SimpleStringProperty(cellData.getValue().getArrivalTime().format(DateTimeFormatter.ofPattern("HH:mm")))
);

TableColumn<Flight, Double> priceColumn = new TableColumn<>("Цена");
priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));

TableColumn<Flight, Integer> seatsColumn = new TableColumn<>("Места");
seatsColumn.setCellValueFactory(new PropertyValueFactory<>("availableSeats"));

flightTable.getColumns().addAll(flightNumberColumn, airlineColumn, departureCityColumn,
arrivalCityColumn, departureDateColumn, arrivalDateColumn,
departureTimeColumn, arrivalTimeColumn, priceColumn, seatsColumn);

Button bookButton = new Button("Забронировать");
bookButton.setOnAction(event -> bookFlight());

// Создание контейнеров
HBox inputBox = new HBox(10);
inputBox.getChildren().addAll(label, databaseNameField);
inputBox.setAlignment(Pos.CENTER);
inputBox.setPadding(new Insets(10));

VBox root = new VBox(10);
root.getChildren().addAll(inputBox, connectButton, flightTable, bookButton);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(20));

// Действие при нажатии на кнопку "Подключиться"
connectButton.setOnAction(event -> {
String databaseName = databaseNameField.getText();
if (databaseName.isEmpty()) {
System.out.println("Введите имя базы данных!");
return;
}
try (Connection connection = DriverManager.getConnection("jdbc:sqlite:" + databaseName)) {
System.out.println("Подключение к базе данных " + databaseName + " успешно установлено!");
loadFlights(connection);
} catch (SQLException e) {
System.out.println("Ошибка подключения к базе данных: " + e.getMessage());
}
});

// Создание сцены
Scene scene = new Scene(root, 800, 600);
primaryStage.setTitle("SQLite JDBC Connect");
primaryStage.setScene(scene);
primaryStage.show();
}

private void loadFlights(Connection connection) {
flightTable.getItems().clear(); // Очистка предыдущих данных
String sql = "SELECT flight_number, airline, departure_city, arrival_city, departure_date, " +
"arrival_date, departure_time, arrival_time, price, seats FROM flights";
try (Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
String flightNumber = rs.getString("flight_number");
String airline = rs.getString("airline");
String departureCity = rs.getString("departure_city");
String arrivalCity = rs.getString("arrival_city");
LocalDate departureDate = LocalDate.parse(rs.getString("departure_date"));
LocalDate arrivalDate = LocalDate.parse(rs.getString("arrival_date"));
LocalTime departureTime = LocalTime.parse(rs.getString("departure_time"));
LocalTime arrivalTime = LocalTime.parse(rs.getString("arrival_time"));
double price = rs.getDouble("price");
int seats = rs.getInt("seats");

Flight flight = new Flight(flightNumber, airline, departureCity, arrivalCity,
departureDate, arrivalDate, departureTime, arrivalTime,
price, seats);
flightTable.getItems().add(flight);
}
} catch (SQLException e) {
e.printStackTrace();
}
}

private void bookFlight() {
Flight selectedFlight = flightTable.getSelectionModel().getSelectedItem();
if (selectedFlight != null) {
System.out.println("Вы забронировали рейс: " + selectedFlight.getFlightNumber());
// Здесь можно добавить логику для бронирования
} else {
System.out.println("Выберите рейс для бронирования.");
}
}

public static void main(String[] args) {
launch(args);
}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.