How can I store a specific date from java to my database? (Not only the date today, but also some dates that the user wants to specifiy)
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse(tf.getText());
String d = dateFormat.format(date);
String str = "insert into tableName(tDate) values (?)";
con = mysqlConnection.dbConnector();
prs = con.prepareStatement(str);
prs.setDate(1, // dont know what to put here);
int rsUpdate = prs.executeUpdate();
con.close();
prs.close();
} catch(ParseException exx) {
System.err.println(exx);
use this it can help
Use
The types
java.sql.Date
andjava.sql.Timestamp
are suppoed to be used to set date and timestamp fields, respectively. Read their documentation for the differences between them and between them andjava.util.Date
.You usually get a java date (
java.util.Date
) from the user. You convert it to milliseconds since the epoch usinggetTime()
, and then convert back to ajava.sql.Date
orjava.sql.Timestamp
as I have shown.