java 如何从结果集中检索列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11361116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to retrieve column value from result set
提问by Sunil Kumar
when i run this program i got this in the output screen.I don't know what is the problem is there any thing i am missing.
当我运行这个程序时,我在输出屏幕中得到了这个。我不知道有什么问题是我遗漏了什么。
import org.apache.cassandra.cql.jdbc.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.*;
import javax.sql.*;
public class Operations
{
public static void main(String[] args){
try
{
Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
Connection con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/temp");
//String name = "name";
String qry = "select name FROM tempcql where key = detail";
Statement smt = con.createStatement();
ResultSet resultSet = smt.executeQuery(qry);
while(resultSet.next())
{
System.out.println(resultSet.getString("name"));
}
}
catch(Exception e)
{
System.out.println(" : "+e.getMessage());
}
}
}
here is the output of this : java.nio.HeapByteBuffer[pos=86 lim=91 cap=159]
这是输出: java.nio.HeapByteBuffer[pos=86 lim=91 cap=159]
回答by kevinpeterson
You could try getting the bytes and converting that to a String:
您可以尝试获取字节并将其转换为字符串:
String name_result = new String(resultSet.getBytes("name"), "UTF-8");
System.out.println(name_result);
回答by kevinpeterson
Try using single quotes in your SQL string:
尝试在 SQL 字符串中使用单引号:
String qry = "select name FROM tempcql where key = 'detail'";
回答by UVM
You are getting the value in the binary format.But you specified column type as string.Instead you need to say as follows:
您正在获取二进制格式的值。但是您将列类型指定为字符串。相反,您需要说如下:
ObjectInputStream is = rs.getBinaryStream("name");
ByetBuffer bb = (ByteBuffer)is.readObject();