[英]Cassandra : How to get the String value of column data irrespective of datatype
I am trying to fetch the results from a com.datastax.driver.core.ResultSet
and print it: 我正在尝试从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)
throws an exception in case of datatypes other than String. 如果数据类型不是String,则row.getString(colIndex)
引发异常。 How will I be able to get a string data irrespective of what data type it is? 无论字符串是什么数据类型,我如何都能得到它?
This code will print the type of each column and value 此代码将打印每列的类型和值
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.