今天在Oracle数据库中查找数据,碰到了一个表中有个字符类型为CHAR[2000],由于字符长,用String无法取出。经过google 百度之后发现可以用CLOB类型来取。具体方法跟用字符串的方法差不多,只不过最后需要将CLOB类型转换成字符串类型。关键代码如下:
CLOB fztdjqs = null;
PrintWriter fztdjqsOut=response.getWriter();
try {
s = myConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
r = s.executeQuery("SELECT * FROM TAB_FZTDJQS where xzcbm="+bianMa);
while (r.next()) {
fztdjqs=(CLOB) r.getClob("FZTD");
System.out.println(fztdjqs.toString());
}
} catch (SQLException e) {
e.printStackTrace();
}
public String ClobToString(CLOB clob) throws SQLException, IOException {
String reString = "";
Reader is = clob.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
return reString;
}
更多Oracle相关信息见 专题页面