Skip to content

Commit 68e96fb

Browse files
Fixes date issues
1 parent f58ff57 commit 68e96fb

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Usage
1717

1818
The library is available on Maven Central.
1919

20+
The last release:
21+
2022
```xml
2123
<dependency>
2224
<groupId>com.qwazr</groupId>
@@ -25,6 +27,16 @@ The library is available on Maven Central.
2527
</dependency>
2628
```
2729

30+
The current Snapshot:
31+
32+
```xml
33+
<dependency>
34+
<groupId>com.qwazr</groupId>
35+
<artifactId>jdbc-cache-driver</artifactId>
36+
<version>1.1-SNAPSHOT</version>
37+
</dependency>
38+
```
39+
2840
### JAVA Code example
2941

3042
First, you have to initialize the JDBC drivers.
@@ -35,12 +47,10 @@ You can use any compliant JDBC driver.
3547
// Initialize the cache driver
3648
Class.forName("com.qwazr.jdbc.cache.Driver");
3749

38-
// Initialize the third-party driver
39-
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
40-
41-
// Provide the URL of the backend driver
50+
// Provide the URL and the Class name of the backend driver
4251
Properties info = new Properties();
4352
info.setProperty("cache.driver.url", "jdbc:derby:memory:myDB;create=true");
53+
info.setProperty("cache.driver.class", "org.apache.derby.jdbc.EmbeddedDriver");
4454

4555
// Get your JDBC connection
4656
Connection cnx = DriverManager.getConnection("jdbc:cache:file:/var/jdbc/cache", info);
@@ -53,7 +63,10 @@ The syntax of the URL is:
5363

5464
*jdbc:cache:file:{path-to-the-cache-directory}*
5565

56-
The property **cache.driver.url** contains the typical JDBC URL of the backend driver.
66+
Two possible properties:
67+
- **cache.driver.url** contains the typical JDBC URL of the backend driver.
68+
- **cache.driver.class** contains the class name of the backend driver.
69+
5770
The properties are passed to both the cache driver and the backend driver.
5871

5972
### Use in transparent mode

src/main/java/com/qwazr/jdbc/cache/CachedResultSet.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.net.URL;
2222
import java.nio.file.Path;
2323
import java.sql.*;
24+
import java.text.DateFormat;
25+
import java.text.ParseException;
2426
import java.util.Calendar;
2527
import java.util.HashMap;
2628
import java.util.Map;
@@ -236,9 +238,15 @@ public Date getDate(int columnIndex) throws SQLException {
236238
return null;
237239
if (val instanceof Date)
238240
return (Date) val;
241+
if (val instanceof Timestamp)
242+
return new Date(((Timestamp) val).getTime());
239243
if (val instanceof Number)
240244
return new Date(((Number) val).longValue());
241-
return new Date(Long.parseLong(val.toString()));
245+
try {
246+
return new Date(DateFormat.getDateInstance().parse(val.toString()).getTime());
247+
} catch (ParseException e) {
248+
throw new SQLException("Unexpected Date type (" + val.getClass() + ") on column " + columnIndex, e);
249+
}
242250
}
243251

244252
@Override
@@ -250,7 +258,11 @@ public Time getTime(int columnIndex) throws SQLException {
250258
return (Time) val;
251259
if (val instanceof Number)
252260
return new Time(((Number) val).longValue());
253-
return new Time(Long.parseLong(val.toString()));
261+
try {
262+
return new Time(DateFormat.getTimeInstance().parse(val.toString()).getTime());
263+
} catch (ParseException e) {
264+
throw new SQLException("Unexpected Time type (" + val.getClass() + ") on column " + columnIndex, e);
265+
}
254266
}
255267

256268
@Override
@@ -262,7 +274,11 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException {
262274
return (Timestamp) val;
263275
if (val instanceof Number)
264276
return new Timestamp(((Number) val).longValue());
265-
return new Timestamp(Long.parseLong(val.toString()));
277+
try {
278+
return new Timestamp(DateFormat.getDateTimeInstance().parse(val.toString()).getTime());
279+
} catch (ParseException e) {
280+
throw new SQLException("Unexpected Timestamp type (" + val.getClass() + ") on column " + columnIndex, e);
281+
}
266282
}
267283

268284
@Override

0 commit comments

Comments
 (0)