将日期从JDateChooser插入Oracle DB

时间:2016-07-28 11:14:48

标签: java oracle java.util.date

我无法将jDateChooser的日期插入到我的oracle db中。以下是staffDisplay类中的action事件。

`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");
            }`

这是准备好的声明在不同的类中。

 `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;
    }`

出现错误信息

   `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)`

所以我认为插入到jdatechooser的日期是作为java.util.date输入的,我需要它是java.sql.date吗?如果这是问题?如果是这样,我如何将其转换为java.sql.date?感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用new java.sql.Date(long milis)并使用startDateDateChooser.getDate().getTime()

获取所选日期的毫秒数