java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1使用Cassandra时间戳列

时间:2016-08-17 11:10:06

标签: java jdbc cassandra

我正在使用JDBC来查询Cassandra。我有Cassandra 3.0和java 1.7.0_101。我无法使用timestamp列进行查询。以下是我的询问:

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的类型为timestamp。如果我在select中只有host_id列,则查询有效。 如果我添加date_filter列,由于类型为timestamp,它会给出错误:

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

任何人都可以帮助我们如何处理使用JDBC的Cassandra时间戳。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

如果您设置日期使用jdbc然后解析为java.sql.Timestamp或java.sql.Date如果您只需要没有时间的日期

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