![](/img/trans.png)
[英]Cassandra/Datastax: How to get values of column having list datatype in java
[英]Cassandra : How to get the String value of column data irrespective of datatype
我正在嘗試從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));
}
}
如果數據類型不是String,則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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.