Thomas Hallgren
unread,Apr 2, 2012, 2:03:29 AM4/2/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to web4j-users
The PostgreSQL driver does not accept String assignements to a
parameter
that is of date or timestamp type. This patch ensures that the correct
java.sql.Date and java.sql.Timestamp types are used.
--------------- src/hirondelle/web4j/database/SqlStatement.java
---------------
diff --git a/src/hirondelle/web4j/database/SqlStatement.java b/src/
hirondelle/web4j/database/SqlStatement.java
index 79258be..1d86bb3 100644
--- a/src/hirondelle/web4j/database/SqlStatement.java
+++ b/src/hirondelle/web4j/database/SqlStatement.java
@@ -13,10 +13,13 @@
import java.math.BigDecimal;
import java.sql.Connection;
+import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.sql.Time;
+import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -299,38 +302,41 @@
private void setDateTime(Object aParam, PreparedStatement
aStatement, int aIdx) throws SQLException {
DateTime dateTime = (DateTime)aParam;
- String formattedDateTime = "";
+
if (
dateTime.unitsAllPresent(Unit.YEAR, Unit.MONTH, Unit.DAY) &&
dateTime.unitsAllAbsent(Unit.HOUR, Unit.MINUTE, Unit.SECOND)
){
fLogger.finest("Treating DateTime as a date (year-month-
day).");
- formattedDateTime =
dateTime.format(DbConfig.getDateFormat(fSqlId.getDatabaseName()));
+ aStatement.setDate(aIdx, new
Date(dateTime.getMilliseconds(TimeZone.getDefault())));
+ return;
}
- else if (
+
+ if (
dateTime.unitsAllAbsent(Unit.YEAR, Unit.MONTH, Unit.DAY) &&
dateTime.unitsAllPresent(Unit.HOUR, Unit.MINUTE, Unit.SECOND)
){
fLogger.finest("Treating DateTime as a time (hour-minute-
second).");
- formattedDateTime =
dateTime.format(DbConfig.getTimeFormat(fSqlId.getDatabaseName()));
+ aStatement.setTime(aIdx, new
Time(dateTime.getMilliseconds(TimeZone.getDefault())));
+ return;
}
- else if (
+
+ if (
dateTime.unitsAllPresent(Unit.YEAR, Unit.MONTH, Unit.DAY) &&
dateTime.unitsAllPresent(Unit.HOUR, Unit.MINUTE, Unit.SECOND)
) {
fLogger.finest("Treating DateTime as a date+time (year-month-
day-hour-minute-second).");
- formattedDateTime =
dateTime.format(DbConfig.getDateTimeFormat(fSqlId.getDatabaseName()));
+ aStatement.setTimestamp(aIdx, new
Timestamp(dateTime.getMilliseconds(TimeZone.getDefault())));
+ return;
}
- else {
- String message =
- "Unable to format DateTime using the
DateTimeFormatForPassingParamsToDb setting in web.xml." +
- " The units present in the DateTime object do not match any
of the expected combinations. " +
- "If needed, you can always format the DateTime manually in
your DAO, and pass a String to the database instead of a DateTime."
- ;
- fLogger.severe(message);
- throw new IllegalArgumentException(message);
- }
- aStatement.setString(aIdx, formattedDateTime);
+
+ String message =
+ "Unable to format DateTime using the
DateTimeFormatForPassingParamsToDb setting in web.xml." +
+ " The units present in the DateTime object do not match any of
the expected combinations. " +
+ "If needed, you can always format the DateTime manually in
your DAO, and pass a String to the database instead of a DateTime."
+ ;
+ fLogger.severe(message);
+ throw new IllegalArgumentException(message);
}