免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1269 | 回复: 0
打印 上一主题 下一主题

Populating an Oracle table with a CLOB column [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-06-16 15:51 |只看该作者 |倒序浏览
Sorry, I can't type Chinese on this machine, and hopefully you can unstand my English.
I need to populate a sample Oracle table, which has at least four columns: filename, createdata, author, and content.
The table stores info for some html files, and the column content will contain the body characters of the html files.
I will do a for loop and open files one by one(up to 500 max), then do a query update. The problem is that I am not familiar
with Oracle SQL programming, especially the CLOB datatype. I am not sure what's the right way to populate a table that contains
a CLOB column. Below is the draft I have, any suggestion or observation? Thanks in advance.



  1. import java.sql.*;
  2. import java.io.*;
  3. //import oracle jdbc driver

  4. //create the tableString cmd = "CREATE TABLE file (filename varchar(30), createdate varchar(10), author varchar(30), content clob)";
  5. stmt.execute (cmd); PreparedStatement pstmt = conn.prepareStatement(""insert into FILE (filename, createdate, author, content) values (?, * ?, ?, ?, ?)");

  6. String fileName;
  7. Date createDate;
  8. String author;
  9. pstmt.setString (1, fileName);  // Set the Bind Value
  10. pstmt.setDATE (2, createDate);  
  11. pstmt.setString (3, author);  
  12. pstmt.setCLOB (4, empty_blob());   

  13. //the top level dir will be command line input
  14. File startDir = new File(args[1]);
  15. if ( !startDir.isDirectory() )
  16. {System.out.println("Please give me a dir!!!\n");
  17. return;
  18. }
  19. File[] contents = startDir.listFiles();
  20. for( int i=0; i<500 && i<contents.length; i++)
  21. {
  22. //open file only and read in data, ignor dirs
  23. if( !contents[i].isDirectory() )
  24. {
  25. fileName = contents[i].getName();
  26. createDate = new Date(contents[i].lastModified());
  27. //author =
  28. CLOB content;
  29. String cmd = "SELECT * FROM file WHERE filename=fileName";
  30. ResultSet rest = stmt.executeQuery(cmd);
  31. CLOB clob = ((OracleResultSet)rset).getCLOB(4);

  32. System.out.println("the length = " + content[i].length());
  33. FileInputStream instream = new FileInputStream(content[i]);
  34. OutputStream outstream = clob.getCharacterOutputStream();
  35. int size = clob.getBufferSize();
  36. byte[] buffer = new byte[size];
  37. int length = -1;
  38. while ((length = instream.read(buffer)) != -1) {
  39. outstream.write(buffer, 0, length);
  40. }
  41. instream.close();
  42. outstream.close();}}
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP