0

I am having trouble inserting a date for the jDateChooser into my oracle db. Below is the action event in the class staffDisplay.

`public void saveButtonPressed(ActionEvent e){
         try{
            int result = staff.addStaff(
                    firstNameTextField.getText(),
                    lastNameTextField.getText(),
                    dateOfBirthDateChooser.getDate(),
                    departmentTextField.getText(),
                    Double.parseDouble(salaryTextField.getText()),
                    startDateDateChooser.getDate(),
                    Boolean.parseBoolean(fullTimeTextField.getText()));

            if(result == 1){//it worked
                JOptionPane.showMessageDialog(this,"Staff Member Added Successfully");
            }else{//didn't work
                JOptionPane.showMessageDialog(this, "Error Occured - Staff member was not added");
            }`

This is the prepared statement is in a different class.

 `public class StaffQueries {
private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
private static final String USERNAME = "xxx";
private static final String PASSWORD = "xxx";

private Connection con;

private PreparedStatement insert = null;

 public StaffQueries() {

    try{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        System.out.println("Drivers loaded and connection made");

        insert = con.prepareStatement("INSERT INTO STAFF " + "(StaffID, FirstName, LastName, DateOfBirth, Department, Salary, StartDate, Fulltime)"
                + "VALUES(ColmStaffSequence.NextVal,?,?,?,?,?,?,?)");

    }catch(SQLException e){
        System.out.println("something went wrong with the DataBase");
        e.printStackTrace();
        System.exit(1);

    }catch(Exception e){
        System.out.println("something went wrong when loading the drivers");
        e.printStackTrace();
        System.exit(2);

    }
}

 public int addStaff(String fn, String ln, Date dob, String d, double sal, Date sd, boolean ft){

        int results = 0;

        try{
            //fill in the missing parameters for the prepared insert statement
            insert.setString(1, fn);
            insert.setString(2, ln);
            insert.setDate(3, dob);
            insert.setString(4, d);
            insert.setDouble(5, sal);
            insert.setDate(6, sd);
            insert.setBoolean(7, ft);

            //execute the prepared insert statement
            results = insert.executeUpdate();
        } catch (SQLException e){
            e.printStackTrace();
            close();
        }
        return results;
    }`

There error message says

   `Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
The method addStaff(java.lang.String, java.lang.String, java.sql.Date, java.lang.String, double, java.sql.Date, boolean) in the type StaffQueries is not applicable for the arguments (java.lang.String, java.lang.String, java.util.Date, java.lang.String, double, java.util.Date, boolean)`

So I think the date insertedd to the jdatechooser is entered as java.util.date and I need it to be java.sql.date? If this is the problem? if so how do I convert it to java.sql.date? Thanks.

Clyo
  • 3
  • 3
  • Change the Import from `java.sql.Date` to `java.util.Date` in StaffQueries – Jens Jul 28 '16 at 11:18
  • I changed this and a new exception comes up. `java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date`. I understand why a util.date cannot be cast to sql.date but I don't know how to correct this in the code. Do you have any suggestions? – Clyo Jul 28 '16 at 13:10

1 Answers1

0

You can use new java.sql.Date(long milis) and getting the milliseconds from the selected date by using startDateDateChooser.getDate().getTime()

Clayn
  • 1,016
  • 9
  • 11