insert jdatechooser from java to database -
how can insert jdatechooser java database? have used solutions of net still can't insert. please me. i'm using eclipse environment. code:
try { preparedstatement stm= (preparedstatement) con.preparestatement("insert d"+ "(dateentré)"+ "values(?)"); ((preparedstatement)stm).setdate(1,convertutildatetosqldate(datechooser.getdate())); statement.execute(); joptionpane.showmessagedialog(null,"added"); } catch (exception e1) { joptionpane.showmessagedialog(null,e1.getmessage()); }
your code example seems wrong, , typo, since you've provided no other information regards error, have go on...
you create preapredstatement
called stmt
, use statement
execute
query.
you should using same variable/instance bind , excute query. should use executeupdate
instead of execute
, provides additional information when statement executed number of rows affected update.
try { preparedstatement stm= (preparedstatement) con.preparestatement("insert d (dateentré) values(?)"); stm.setdate(1,convertutildatetosqldate(datechooser.getdate())); int rows = statement.executeupdate(); if (rows > 0) { // should 1 joptionpane.showmessagedialog(null,"added"); } else { joptionpane.showmessagedialog(null,"update failed unknown reason"); } } catch (exception e1) { joptionpane.showmessagedialog(null,e1.getmessage()); e1.printstacktrace(); }
this of course assumes database column compatible type of java.sql.date
take closer @ jdbc database access , using prepared statements more details.
Comments
Post a Comment