#java.sql.NClob
# 描述
NClob接口用于操作NCLOB类型数据。
java.sql.NClob是JDBC规范中用于操作数据库国家字符集大字符对象的接口。NClob继承java.sql.Clob,自身没有新增方法,因此方法名称、参数类型和返回类型与Clob相同。
详细接口定义请参考JDBC官方网站 (opens new window)。
# 方法
YashanDB JDBC驱动对NCLOB接口有如下方法(单机部署/共享集群/分布式集群部署):
| 方法 | 返回类型 |
|---|---|
| length() | long |
| getSubString(long pos, int length) | String |
| setString(long pos, String str) | int |
| setString(long pos, String str, int offset, int len) | int |
| setAsciiStream(long pos) | java.io.OutputStream |
| setCharacterStream(long pos) | java.io.Writer |
| getCharacterStream() | java.io.Reader |
| getCharacterStream(long pos, long length) | Reader |
| getAsciiStream() | java.io.InputStream |
| free() | void |
| truncate(long len) | void |
YashanDB JDBC驱动对NCLOB接口有如下方法(存算一体分布式集群部署):
| 方法 | 返回类型 | 备注 |
|---|---|---|
| length() | long | - |
| getSubString(long pos, int length) | String | - |
| setString(long pos, String str) | int | - |
| setString(long pos, String str, int offset, int len) | int | 存算一体分布式集群部署中只能调用本接口将数据写入至CN节点的临时LOB中,无法将数据写至DN节点上。 |
| getCharacterStream() | java.io.Reader | - |
| getAsciiStream() | java.io.InputStream | - |
| setAsciiStream(long pos) | java.io.OutputStream | - |
| setCharacterStream(long pos) | java.io.Writer | - |
| getCharacterStream(long pos, long length) | Reader | - |
| truncate(long len) | void | 存算一体分布式集群部署中只能调用本接口将数据写入至CN节点的临时LOB中,无法将数据写至DN节点上。 |
| free() | void | - |
说明:
- YashanDB JDBC 1.10.3不支持
position(String searchstr, long start)和position(Clob searchstr, long start)方法,调用时抛出SQLFeatureNotSupportedException。
# 示例
以下示例使用createNClob()创建NCLOB对象,通过setCharacterStream()写入Unicode文本,使用setNClob()保存到数据库,并通过getNClob()读取内容。
package quickstart;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class NClobInterfaceExample {
private static final String URL =
"jdbc:yasdb://192.168.1.2:1688/yasdb";
private static final String TABLE = "jdbc_nclob_example";
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("user", "username");
properties.setProperty("password", "passwd");
Class.forName("com.yashandb.jdbc.Driver");
try (Connection connection =
DriverManager.getConnection(URL, properties)) {
try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE IF EXISTS " + TABLE);
statement.execute(
"CREATE TABLE " + TABLE
+ " (id INTEGER PRIMARY KEY, content NCLOB)");
}
NClob nclob = connection.createNClob();
try (Writer writer = nclob.setCharacterStream(1)) {
writer.write("YashanDB JDBC NCLOB示例");
}
try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO " + TABLE + " VALUES (?, ?)")) {
statement.setInt(1, 1);
statement.setNClob(2, nclob);
statement.executeUpdate();
}
nclob.free();
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"SELECT content FROM " + TABLE + " WHERE id = 1")) {
if (resultSet.next()) {
NClob storedNClob = resultSet.getNClob(1);
try (Reader reader =
storedNClob.getCharacterStream()) {
StringBuilder content = new StringBuilder();
char[] buffer = new char[1024];
int count;
while ((count = reader.read(buffer)) != -1) {
content.append(buffer, 0, count);
}
System.out.println("NCLOB content: " + content);
}
storedNClob.free();
}
}
} finally {
try (Connection connection =
DriverManager.getConnection(URL, properties);
Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE IF EXISTS " + TABLE);
}
}
}
}
正常情况下,程序输出如下:
NCLOB content: YashanDB JDBC NCLOB示例

