gpt4 book ai didi

java - Cassandra : How to get the String value of column data irrespective of datatype

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:44 25 4
gpt4 key购买 nike

我正在尝试从 com.datastax.driver.core.ResultSet 获取结果并打印它:

ResultSet results = getSession().execute("Select * from test.table"); //getsession returns a session
int numcols = results.getColumnDefinitions().size();
for ( Row row : results ) {
for (int colIndex = 0; colIndex < numcols; colIndex++) {
System.out.println("data: " + row.getString(colIndex));
}
}

row.getString(colIndex) 如果数据类型不是字符串,则会引发异常。无论数据类型是什么,我如何才能获取字符串数据?

最佳答案

此代码将打印每列和值的类型

ResultSet results = getSession().execute("Select * from test.table");    
int numcols = results.getColumnDefinitions().size();
for ( Row row : results ) {
for (int colIndex = 0; colIndex < numcols; colIndex++) {
System.out.println("Type : "+row.getColumnDefinitions().getType(colIndex));
Class c = row.getColumnDefinitions().getType(colIndex).asJavaClass();
if(c == Integer.class) {
System.out.println("Data :" + row.getInt(colIndex));
}
//similar for other type
}
}

关于java - Cassandra : How to get the String value of column data irrespective of datatype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35666674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com