aaberg / sql2o

sql2o is a small library, which makes it easy to convert the result of your sql-statements into objects. No resultset hacking required. Kind of like an orm, but without the sql-generation capabilities. Supports named parameters.
http://sql2o.org
MIT License
1.15k stars 229 forks source link

Saving Dates #305

Open fredericoregateiro opened 6 years ago

fredericoregateiro commented 6 years ago

It is possible to save da Date property like a date on a database instead of a timestamp? In my class i have:

    private Date systemEntryDate;

    public Date getSystemEntryDate() {
        return systemEntryDate;
    }

    public void setSystemEntryDate(Date systemEntryDate) {
        this.systemEntryDate = systemEntryDate;
    }

and the database i have to use is a existing sqlite3 db, containing the values on the SystemEntryDate column like 2018-10-23 10:16:27.8871829

and the values stored in this column by sql2o is a timestamp like 1540306987843.

Can i change this behavior, and store a date?

fredericoregateiro commented 6 years ago

The only way i was able to accomplish this was to create a formater and use the format as a parameter:

SimpleDateFormat dateTimeformatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

PreparedStatement stmt = connection.prepareStatement(query);
stmt.setObject(1, dateTimeformatter.format(date), java.sql.Types.DATE);

and with sqlo like this:

transaction.createQuery(query)
.addParameter("SystemEntryDate", dateTimeformatter.format(document.getSystemEntryDate()))
.executeUpdate()

Is there a better way?