Reputation: 2324
I'm using a database in my Java project and I want to store date in it, the 5th and the 6th parameter are Date Object. I used the solution below but I have errors in the indicated lines:
PreparedStatement creerFilm = connecteur.getConnexion().prepareStatement(
"INSERT INTO FILM (ID, REF, NOM, DISTRIBUTEUR, DATEDEBUT, DATEFIN) "+
"VALUES (?, ?, ?, ?, ?, ?)");
creerFilm.setInt(1, getId());
creerFilm.setString(2, getReference());
creerFilm.setString(3, getNomFilm());
creerFilm.setString(4, getDistributeur());
// These next two lines
creerFilm.setDate(5, new Date (getDateDebut().getDate()));
creerFilm.setDate(6, new Date (getDateFin().getDate()));
// The above two lines
creerFilm.executeUpdate();
creerFilm.close();
Can you help me to fix that please ?
Thank you
Upvotes: 2
Views: 8546
Reputation: 4190
I think detailed answer you can read here: How to insert date in sqlite through java
In a short, you can insert Date as setString (or setInt, or setLong (see the above link), but not setDate):
PreparedStatement ps = connection.prepareStatement(<your sql>);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ps.setString(1, df.format(<your date>));
Upvotes: 1
Reputation: 51445
I can't really tell from your code, but you have to use java.sql.Date, not java.util.Date.
Here's how you convert from a utility date instance to an SQL date instance:
java.sql.Date date = new java.sql.Date(utilDate.getTime());
Upvotes: 6