如何从结果集中检索列值

时间:2012-07-06 11:12:43

标签: java cassandra cql jdbc-odbc

当我运行这个程序时,我在输出屏幕中得到了这个。我不知道有什么问题我有什么问题。

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());
        }
            }
}

这是输出:java.nio.HeapByteBuffer [pos = 86 lim = 91 cap = 159]

3 个答案:

答案 0 :(得分:1)

您可以尝试获取字节并将其转换为字符串:

String name_result = new String(resultSet.getBytes("name"), "UTF-8");
System.out.println(name_result);

答案 1 :(得分:0)

尝试在SQL字符串中使用单引号:

String qry = "select name FROM tempcql where key = 'detail'";

答案 2 :(得分:0)

您正在以二进制格式获取值。但是您将列类型指定为字符串。相反,您需要说明如下:

ObjectInputStream is = rs.getBinaryStream("name");
ByetBuffer bb = (ByteBuffer)is.readObject();