0% found this document useful (0 votes)
5 views

PSS

The document contains Java methods for managing transport data in a database. The 'availableTransportData' method retrieves transport records and parses date fields, while the 'availableTransportAdd' method handles the addition of new transport entries, including validation checks for required fields and existing IDs. Alerts are used to inform users of errors or successful operations during data entry and updates.

Uploaded by

nadia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PSS

The document contains Java methods for managing transport data in a database. The 'availableTransportData' method retrieves transport records and parses date fields, while the 'availableTransportAdd' method handles the addition of new transport entries, including validation checks for required fields and existing IDs. Alerts are used to inform users of errors or successful operations during data entry and updates.

Uploaded by

nadia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

private ObservableList<Transport> availableTransportData() {

ObservableList<Transport> transportList =
FXCollections.observableArrayList();
String query = "SELECT * FROM transport";

try (Statement stmt = connect.createStatement(); ResultSet rs =


stmt.executeQuery(query)) {
while (rs.next()) {
// Parse the Date field from String to Date
String dateString = rs.getString("Date"); // assuming the column
name is "Date"
Date transportDate = null;
if (dateString != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
transportDate = (Date) sdf.parse(dateString);
}

Transport transport = new Transport(


TransportType.valueOf(rs.getString("type")),
rs.getString("companyName"),
rs.getString("departureLocation"),
rs.getString("arrivalLocation"),
transportDate, // Use the Date object here
rs.getString("departureTime"),
rs.getString("arrivalTime"),
rs.getDouble("price"),
rs.getBoolean("isElectric"),
rs.getBoolean("availability")
);
transportList.add(transport);
}
} catch (SQLException | ParseException e) {
e.printStackTrace();
}
return transportList;
}

////////////////////////////////////////////////////////////////////////////////

public void availableTransportAdd() {


String addData = "INSERT INTO transport (idTransport, type, companyName,
departureLocation, arrivalLocation, Date, departureTime, arrivalTime, price,
isElectric, availability) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

Alert alert;
try {

// Check if any required fields are empty


if (transport_type.getSelectionModel().getSelectedItem() == null
|| companyName.getText().isEmpty() ||
DP_location.getText().isEmpty() || AR_location.getText().isEmpty()
|| date_id.getValue() == null || DP_time.getText().isEmpty() ||
AR_time.getText().isEmpty() || price_id.getText().isEmpty()
|| electric_type.getSelectionModel().getSelectedItem() == null
|| availibility_id.getSelectionModel().isEmpty()) {

alert = new Alert(Alert.AlertType.ERROR);


alert.setTitle("Error Message");
alert.setHeaderText(null);
alert.setContentText("Please fill all blank fields!");
alert.showAndWait();
return;
}

// Check if Transport ID exists


String check = "SELECT idTransport FROM transport WHERE idTransport
= ?";
PreparedStatement prepare = connect.prepareStatement(check);
prepare.setInt(1, Integer.parseInt(availableT_transportID.getText()));
ResultSet result = prepare.executeQuery();

if (result.next()) {
alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error Message");
alert.setHeaderText(null);
alert.setContentText("Transport ID: " +
availableT_transportID.getText() + " already exists!");
alert.showAndWait();
} else {
// Prepare SQL statement to insert the new transport
prepare = connect.prepareStatement(addData);

// Get the Date from the DatePicker (convert to LocalDate)


LocalDate transportDate = date_id.getValue(); // This returns a
LocalDate
if (transportDate == null) {
alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error Message");
alert.setHeaderText(null);
alert.setContentText("Please select a valid date!");
alert.showAndWait();
return;
}

// Convert LocalDate to java.sql.Date


java.sql.Date sqlTransportDate =
java.sql.Date.valueOf(transportDate);

// Set parameters for the SQL insert statement


prepare.setString(2,
transport_type.getSelectionModel().getSelectedItem().toString());
prepare.setString(3, companyName.getText());
prepare.setString(4, DP_location.getText());
prepare.setString(5, AR_location.getText());
prepare.setDate(6, sqlTransportDate); // Insert the Date
(transport date)
prepare.setString(7, DP_time.getText()); // Departure time
prepare.setString(8, AR_time.getText()); // Arrival time
prepare.setDouble(9, Double.parseDouble(price_id.getText())); //
Price
prepare.setBoolean(10,
electric_type.getSelectionModel().getSelectedItem().equals("Yes")); // Electric
type
prepare.setBoolean(11,
availibility_id.getSelectionModel().getSelectedItem().equals("Available")); //
Availability

// Execute the update


prepare.executeUpdate();

alert = new Alert(Alert.AlertType.INFORMATION);


alert.setTitle("Information Message");
alert.setHeaderText(null);
alert.setContentText("Successfully Added Transport!");
alert.showAndWait();

// Optionally, call the method to refresh the transport data


availableTransportShowData();
}
} catch (Exception e) {
e.printStackTrace();
alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText("An error occurred while adding the transport.");
alert.showAndWait();
}
}

You might also like