// Load the database details into the variables.
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
// Create the properties object that holds all database details
Properties props = new Properties();
props.put("user", user );
props.put("password", password);
props.put("SetBigStringTryClob", "true");
try {
DriverManager.registerDriver(new OracleDriver());
Connection conn = DriverManager.getConnection( url , props );
// Create a PreparedStatement object
PreparedStatement pstmt = null;
// Create a ResultSet to hold the records retrieved.
ResultSet rset = null;
// Create SQL query statement to retrieve records having CLOB data from
// the database.
String sqlCall = query;
pstmt= conn.prepareStatement(sqlCall);
// Execute the PrepareStatement
rset = pstmt.executeQuery();
//String clobVal = null;
// Get the CLOB value larger than 32765 bytes from the resultset
while (rset.next()) {
String clobVal = rset.getString(1);
System.out.println(clobVal);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我尝试使用JDBC将Clob数据转换为String。我有上面的代码,但它从数据库返回null,我不明白为什么。 我在oracle中创建了一个函数,它可以很好地转换,但处理速度很慢。
如何使用JDBC从Clob创建长字符串?我使用的是oracle10g
答案 0 :(得分:0)
查看https://docs.oracle.com/javase/tutorial/jdbc/basics/blob.html#retrieve_clob
您应该使用ResultSet对象中的方法getClob(int pos)。 从Clob对象中,您可以使用getCharacterStream()或getSubString()方法获取Reader或String。