如何使用 java 从 Oracle 检索 CLOB 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19486648/
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 Retrive the CLOB value from Oracle using java
提问by user1882624
SELECT DESCRIPTION,DETAILED_DESCRIPTION,PRIORITY,RISK_LEVE FROM Table_Name
The DETAILED_DESCRIPTION
column is having value in CLOB
该DETAILED_DESCRIPTION
列的值在CLOB
Below is the code is used to fetch the data: But i am getting the error "Error: Read error" while reading the field "DETAILED_DESCRIPTION"
下面是用于获取数据的代码:但我在读取字段“DETAILED_DESCRIPTION”时收到错误“错误:读取错误”
Statement statement;
ResultSet resultSet;
oracleCon.setAutoCommit(false);
statement = oracleCon.createStatement();
String chdet[] = new String[8];
String query="SELECT DESCRIPTION,DETAILED_DESCRIPTION,PRIORITY,RISK_LEVEL FROM Table_Name";
resultSet = statement.executeQuery(query);
ArrayList<String> record=new ArrayList<String>();
while (resultSet.next())
{
record.add(resultSet.getString("DESCRIPTION"));
record.add(resultSet.getString("DETAILED_DESCRIPTION"));
record.add(resultSet.getString("PRIORITY"));
record.add(resultSet.getString("RISK_LEVEL"));
}
if(record.size()>0)
{
chdet[0] = record.get(0);
chdet[1] = record.get(1);
chdet[2] = record.get(2);
chdet[3] = record.get(3);
break;
}
}
return chdet;
采纳答案by Scary Wombat
After retrieving your data, you can use the getClob () method to return your Clob. Then you needs to open the Clob's stream to read the data (Mayb be char or binary data).
检索数据后,您可以使用 getClob() 方法返回您的 Clob。然后需要打开Clob的流来读取数据(可能是char或者binary数据)。
If the clob is known to be a plain string, you maybe also wish to use
如果已知 clob 是纯字符串,您可能还希望使用
clob.getSubString(1, (int) clob.length());
clob.getSubString(1, (int) clob.length());
So try this
所以试试这个
Clob clob = resultSet.getClob("DETAILED_DESCRIPTION")
record.add(clob.getSubString(1, (int) clob.length());
see http://www.java2s.com/Code/JavaAPI/java.sql/ResultSetgetClobintcolumnIndex.htm
见http://www.java2s.com/Code/JavaAPI/java.sql/ResultSetgetClobintcolumnIndex.htm
回答by Dark Knight
This might help you,
这可能对你有帮助
// Select LOB locator into standard result set.
ResultSet rs =
stmt.executeQuery ("SELECT blob_col, clob_col FROM lob_table");
while (rs.next())
{
// Get LOB locators into Java wrapper classes.
java.sql.Blob blob = rs.getBlob(1);
java.sql.Clob clob = rs.getClob(2);
}
Refer to below link for more details, http://docs.oracle.com/cd/A84870_01/doc/java.816/a81354/oralob2.htm
有关更多详细信息,请参阅以下链接, http://docs.oracle.com/cd/A84870_01/doc/java.816/a81354/oralob2.htm