0

I'm working on a project that requires me to allow the user to select three different values from JComboBoxes, then take the selected values and create a record in a database table when a button is pressed. I'm having trouble when trying to insert the values into the record, as the first two are strings, and the third is a date.

When the button is pressed, the method to add the record is called, sending the values to that method to be added.

Below is the constructor code for adding the record:

    public BookingQueries()
{
    //Establish database connection
    getConnection();
    BookingEntry entry = new BookingEntry();

    try
    {
        insertNewBooking = connection.prepareStatement("INSERT INTO Bookings (Customer, Flight, Day) VALUES(?,?,?)");
    }
    catch (SQLException sqlException)
    {
        sqlException.printStackTrace();
        System.exit(1);
    }
}

This is the code in the method being called to add the record:

    public void addBooking(String customer, String flight, Date day)
{
    try {
        insertNewBooking.setString(1,customer);
        insertNewBooking.setString(2, flight);
        insertNewBooking.setDate(3, (java.sql.Date) day);
        insertNewBooking.executeUpdate();
    }
    catch (SQLException sqlException) 
    {
        sqlException.printStackTrace();
    }       

}

I'm getting an exception saying "java.lang.String cannot be cast to java.util.date".

I'm fairly new to working with databases in Java, but the way that I'm interpreting that is the issue is with adding the "Date" value, which should be added to the SQL database as a Date type. If someone can provide some insight as to how to fix this or corrections to my thinking that I'm just overseeing, it'd be greatly appreciated.

EDIT:

Here is the database table that I'm trying to add to: The CUSTOMER and FLIGHT Columns are VARCHAR, the DAY Column is type DATE

The console says that the error is being caused in the ActionPerformed method when the button is pressed. This is the code for that:

    private void bookingBtnActionPerformed(java.awt.event.ActionEvent evt) {                                           
    Customer customer = new Customer();
    Flight flight = new Flight();
    Day day =  new Day();
    BookingEntry entry = new BookingEntry();
    BookingQueries booking = new BookingQueries();

    String selectedCustomer = (String) customerComboBox.getSelectedItem();
    entry.setCustomer(selectedCustomer);

    String selectedFlight = (String) flightComboBox.getSelectedItem();
    entry.setFlight(selectedFlight);

    Date selectedDate = (Date) dayComboBox.getSelectedItem();
    entry.setDay(selectedDate);

    booking.addBooking(entry.getCustomer(),entry.flight(),entry.day());

}  

Specifically at this line:

   Date selectedDate = (Date) dayComboBox.getSelectedItem();
AngeloKwak
  • 81
  • 5
  • Please show us how the database and its tables are created. –  Apr 12 '18 at 20:01
  • 1
    What line causes the exception/error to occur? You're not telling us this. Could it be where you're calling `addBooking(...)`? Please take care to show relevant code, preferably a [mcve] or as close to a [MCVE](https://stackoverflow.com/help/mcve) as possible. – Hovercraft Full Of Eels Apr 12 '18 at 20:07
  • I've added a screenshot of the table being used, in addition to the code where it says the error is occurring. It specifically seems to be where I'm using getSelectedItem to take the Date value from the ComboBox – AngeloKwak Apr 12 '18 at 20:12
  • 2
    Well what does this JComboBox hold? Come on, please improve this question. Let's not have to keep extracting information from you in dribs and drabs. It likely holds Strings, so yes, your cast won't make sense. Don't have it hold Strings then, and instead fill it with `java.sql.Date` objects. Or create code to convert the object returned to a `java.sql.Date` object. – Hovercraft Full Of Eels Apr 12 '18 at 20:16
  • 1
    I believe I found the issue. The JComboBox being used is being filled with the values directly taken from the Day table that I am using storing the dates, however I was adding the items to the combobox by using "dayComboBox.addItem(resultSet.getString("Date")". Thank you for the critique. – AngeloKwak Apr 12 '18 at 20:24

0 Answers0