不能这么取值,给你个例子看看吧package clob;import java.sql.*;
import java.io.Writer;
import java.io.Reader;
import java.io.BufferedReader;public class TestClob {
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null; public void test(String content) throws Exception { String driver = "oracle.jdbc.driver.OracleDriver";
String url = "dataBase=jdbc:oracle:thin:@localhost:1521:cms";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, "scott", "tiger");
con.setAutoCommit(false);
ps = con.prepareStatement(
"insert into test(id,clobcol) values(1,EMPTY_CLOB())");//先插入一个空值 ps.executeQuery(); String sql = "select clobcol from test where id=? for update"; ps = null;
ps = con.prepareStatement(sql);
ps.setInt(1, 1);
oracle.jdbc.OracleResultSet result = (oracle.jdbc.OracleResultSet) ps.
executeQuery();
oracle.sql.CLOB clob = null;
while (result.next()) {
clob = (oracle.sql.CLOB) result.getClob("clobcol");
}
Writer out = clob.getCharacterOutputStream();
out.write(content);
out.flush();
out.close();
con.commit();
result.close();
ps.close(); }
catch (SQLException ex) {
ex.printStackTrace();
con.rollback();
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
finally {
con.setAutoCommit(true);
con.close();
}
} public void getclob() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "dataBase=jdbc:oracle:thin:@localhost:1521:cms";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, "scott", "tiger");
con.setAutoCommit(false);
String sql = "select clobcol from test where id=1";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
String content=new String();
while (rs.next()) {
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("clobcol");
if (clob != null) {
Reader is = clob.getCharacterStream();
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
while (s != null) {
content += s ;
s = br.readLine();
}
}
System.out.println(content); }
}
catch (SQLException ex) {ex.printStackTrace();
}
catch (ClassNotFoundException ex) {ex.printStackTrace();
}
} public static void main(String[] args) {
try {
TestClob obj=new TestClob();
String content = "hello,world!";
obj.test(content);
obj.getclob();
}
catch (Exception ex) {
ex.printStackTrace();
}
}}
import java.io.Writer;
import java.io.Reader;
import java.io.BufferedReader;public class TestClob {
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null; public void test(String content) throws Exception { String driver = "oracle.jdbc.driver.OracleDriver";
String url = "dataBase=jdbc:oracle:thin:@localhost:1521:cms";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, "scott", "tiger");
con.setAutoCommit(false);
ps = con.prepareStatement(
"insert into test(id,clobcol) values(1,EMPTY_CLOB())");//先插入一个空值 ps.executeQuery(); String sql = "select clobcol from test where id=? for update"; ps = null;
ps = con.prepareStatement(sql);
ps.setInt(1, 1);
oracle.jdbc.OracleResultSet result = (oracle.jdbc.OracleResultSet) ps.
executeQuery();
oracle.sql.CLOB clob = null;
while (result.next()) {
clob = (oracle.sql.CLOB) result.getClob("clobcol");
}
Writer out = clob.getCharacterOutputStream();
out.write(content);
out.flush();
out.close();
con.commit();
result.close();
ps.close(); }
catch (SQLException ex) {
ex.printStackTrace();
con.rollback();
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
finally {
con.setAutoCommit(true);
con.close();
}
} public void getclob() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "dataBase=jdbc:oracle:thin:@localhost:1521:cms";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, "scott", "tiger");
con.setAutoCommit(false);
String sql = "select clobcol from test where id=1";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
String content=new String();
while (rs.next()) {
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("clobcol");
if (clob != null) {
Reader is = clob.getCharacterStream();
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
while (s != null) {
content += s ;
s = br.readLine();
}
}
System.out.println(content); }
}
catch (SQLException ex) {ex.printStackTrace();
}
catch (ClassNotFoundException ex) {ex.printStackTrace();
}
} public static void main(String[] args) {
try {
TestClob obj=new TestClob();
String content = "hello,world!";
obj.test(content);
obj.getclob();
}
catch (Exception ex) {
ex.printStackTrace();
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货