2016-08-17 69 views
0

我正在使用JDBC查询Cassandra。我有Cassandra 3.0和java 1.7.0_101。我无法使用时间戳列进行查询。下面是我的查询:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1使用Cassandra时间戳列时

PreparedStatement pstmt = null; 
Date startDate = Date.valueOf("2016-01-22"); 
Date endDate = Date.valueOf("2016-01-25"); 

pstmt = con.prepareStatement("select host_id,date_filter from btc_hourly_by_em where host_id = ? and date_filter >= ? and date_filter <= ? order by date_filter;"); 

pstmt.setInt(1, 1); 
pstmt.setDate(2, startDate); 
pstmt.setDate(3, endDate); 

ResultSet rs = pstmt.executeQuery(); 

哪里HOST_ID是int类型和date_filter的类型是时间戳。如果我在我的select中只有host_id列,查询就可以工作。 如果我添加date_filter列,由于输入时间戳它给我的错误是:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 

谁能帮助我们如何应对卡桑德拉时间戳和JDBC。任何帮助,将不胜感激

回答

0

如果你设置那么日期使用JDBC解析到的java.sql.Timestamp或java.sql.Date,如果你只需要日期不受时间

pstmt.setDate(2, new java.sql.Timestamp(startDate.getTime())); 
+0

你说的setDate与时间戳工作时在参数....你不认为它的不兼容? – SnehaT

+0

我已将setDate修改为setTimestamp,它仍然给出相同的错误....不起作用 – SnehaT

+0

JDBC驱动程序必须支持'TIMESTAMP'列上的'setDate',并进行必要的转换。 –

相关问题