我在我的Java项目中使用数据库,我想在其中存储日期,第5个和第6个参数是日期对象。我使用了下面的解决方案,但在指示的行中有错误:
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();
你能帮我解决这个问题吗?
谢谢
最佳答案
我无法从你的代码中看出,但你必须使用 java.sql.Date,而不是 java.util.Date。
以下是从实用程序日期实例转换为 SQL 日期实例的方法:
java.sql.Date date = new java.sql.Date(utilDate.getTime());
关于java - 将日期对象存储在sqlite数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2934779/