gpt4 book ai didi

java - 如何将serialClob转换为String格式?

转载 作者:行者123 更新时间:2023-12-02 00:38:09 25 4
gpt4 key购买 nike

我正在尝试将serialclob 转换为字符串。但我没有成功。我认为我没有以正确的方式做事。我的代码是:

char[] buffer;
int count = 0;
int length = 0;
String data = null;
String[] type;
StringBuffer sb;
try{
SqlRowSet rows = getJdbcTemplate().queryForRowSet("select c.BOUNDING_BOX.GET_WKT() BOUNDING_BOX FROM EXAMPLE c WHERE EXAMPLE_ID = 100",
new Object[] {subid});
SubscriptionModel subscription = new SubscriptionModel();
System.out.println("bbox");
SqlRowSetMetaData rsmd = rows.getMetaData();
type = new String[rsmd.getColumnCount()];
for(int col=0;col<rsmd.getColumnCount();col++)
type[col] = rsmd.getColumnTypeName(col + 1);
System.out.println("Read rows and only serial clob data type values");
while(rows.next()){
System.out.println("test 1 here");
for(int col=0;col<rsmd.getColumnCount();col++){
System.out.println("test 2 here");
if("CLOB".equals(type[col]){
System.out.println("test 3 here");
SerialClob clob = (SerialClob) ((ResultSet) rows).getClob(col + 1);
if(clob != null){
System.out.println("clob is not null");
Reader is = clob.getCharacterStream();

sb = new StringBuffer();
length = (int) clob.length();

if(length>0){
buffer = new char[length];
count = 0;
try{
while((count = is.read(buffer)) != -1)
sb.append(buffer);

data = new String(sb);
}catch(Exception e){

}
}
else
data = (String) null;
}else
data = (String) null;
}else{
data = (String) rows.getObject(col + 1);
}
}
}
return subscription;
}catch(Exception e){
e.printStackTrace();
}

但我收到如下错误:

bbox
Read rows and only serial clob data type values
test 1 here
test 2 here
java.lang.ClassCastException: javax.sql.rowset.serial.SerialClob cannot be cast to java.lang.String

我的错误在哪里?

最佳答案

这至少是一个问题:

if(type[col] == "CLOB")

这是直接比较字符串引用。我怀疑这会更好:

if ("CLOB".equals(type[col]))

这将使您进入正确的 if block 。我没有详细查看您的其余代码 - 如果您可以将其分解为更小的方法,那么阅读和维护会更简单。

关于java - 如何将serialClob转换为String格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7207203/

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