java - Inserting date from JDateChooser to Oracle DB -
i having trouble inserting date jdatechooser oracle db. below action event in 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 not added"); }`
this prepared statement in 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 , connection made"); insert = con.preparestatement("insert staff " + "(staffid, firstname, lastname, dateofbirth, department, salary, startdate, fulltime)" + "values(colmstaffsequence.nextval,?,?,?,?,?,?,?)"); }catch(sqlexception e){ system.out.println("something went wrong database"); e.printstacktrace(); system.exit(1); }catch(exception e){ system.out.println("something went wrong when loading 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 missing parameters 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 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: method addstaff(java.lang.string, java.lang.string, java.sql.date, java.lang.string, double, java.sql.date, boolean) in type staffqueries not applicable arguments (java.lang.string, java.lang.string, java.util.date, java.lang.string, double, java.util.date, boolean)`
so think date insertedd jdatechooser entered java.util.date , need java.sql.date? if problem? if how convert java.sql.date? thanks.
you can use new java.sql.date(long milis)
, getting milliseconds selected date using startdatedatechooser.getdate().gettime()
Comments
Post a Comment