先插一个空的clob对象,然后选出来进行读写
解决方案 »
- dbca 出现ORA_01031:insufficient privileges
- ORA-01008 有变量没关联
- 去重复记录问题
- SQL 查询结果有部分隐藏。
- 查询条件为日期的问题?
- 请教Range-Partitioned Index-Organized Tables??分数不够可以加
- 怎样将含有LONG型字段的表的数据卸出来?
- 在Oracle8.17中如何修改internal上的口令?
- 请问各位每隔一分钟执行一个存储过程的Job如何写?要求时间间隔一定是分钟,一秒也不差
- 急!如何在sql语句中对某个字段进行位或操作?
- ***如何实现一个字段在添加时自动加1呢???***
- oracle中如何定义整型字段?
import java.io.*;import oracle.jdbc.*;
import oracle.jdbc.driver.*;
import java.sql.*;public class OraClob {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:oci8:@sid", "username", "password");
conn.setAutoCommit(false);
String sql = "insert into clobdata values(2, empty_clob())";
//Statement stmt = conn.createStatement ();
PreparedStatement stmt = conn.prepareStatement(sql);
//Statement stmt = conn.createStatement();
//stmt.executeUpdate(sql);
stmt.execute(sql);
//conn.commit();
String sql2="select content from clobdata where id=2 for update";
ResultSet rs = stmt.executeQuery(sql2);
System.out.println("inserting...");
if(rs.next()){
File originVR=new File("/home/oracle/pro.txt");
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("content");
/*先建立一个由文件而来的输入流,然后把它一行一行的读入到clob中
*
* */
BufferedReader in = new BufferedReader(new InputStreamReader(
new BufferedInputStream(
new FileInputStream(originVR))));
/*
* 把输入流中的内容转入clob的输出流
* */
StringBuffer strBuf=new StringBuffer();
//String bc=null;
System.out.println("swapping the chars from file to clob");
//int i=0;
long =1;
String tmp="";
while((tmp = in.readLine())!=null){
// write this line of chars into a string buffer
System.out.println(tmp);
//clob.putString(,tmp);
clob.putString(,tmp);
+=tmp.length();
}
// clob.putString(1,"hello world this si the first test of the jdbc of clob");
// clob.pubString(1,in.readLine());
// clob.putString(1,in.readLine());
// put the stringBuffer's contents chars to the clob object
//clob.putString(1,strBuf.toString());
System.out.println("finished the swaping work!");
sql = "update clobdata set content=? where id=2";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setClob(1, clob);
pstmt.executeUpdate();
System.out.println("finished the insert work from the file to clob in oracle .");
System.out.println("the clob area is longth :"+clob.length());
in.close();
conn.close();
//outClob.close();
}
}catch(SQLException e){
System.out.println("mistake when operating the database!");
System.out.println(e.getMessage());
}
catch(FileNotFoundException e){
System.out.println("the file can't be find!");
System.out.println(e.getMessage());
}catch(ClassNotFoundException e){
System.out.println("the oracle jdbc is not found!");
System.out.println(e.getMessage());
}catch (IOException e){
System.out.println("the file stream can't cractly excute!");
}catch (Exception e){
System.out.println("other exception happens!");
System.out.println(e.getMessage());
}
}
}