- 论坛徽章:
- 0
|
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.
- import java.sql.*;
- import java.io.*;
- //import oracle jdbc driver
- //create the tableString cmd = "CREATE TABLE file (filename varchar(30), createdate varchar(10), author varchar(30), content clob)";
- stmt.execute (cmd); PreparedStatement pstmt = conn.prepareStatement(""insert into FILE (filename, createdate, author, content) values (?, * ?, ?, ?, ?)");
- String fileName;
- Date createDate;
- String author;
- pstmt.setString (1, fileName); // Set the Bind Value
- pstmt.setDATE (2, createDate);
- pstmt.setString (3, author);
- pstmt.setCLOB (4, empty_blob());
- //the top level dir will be command line input
- File startDir = new File(args[1]);
- if ( !startDir.isDirectory() )
- {System.out.println("Please give me a dir!!!\n");
- return;
- }
- File[] contents = startDir.listFiles();
- for( int i=0; i<500 && i<contents.length; i++)
- {
- //open file only and read in data, ignor dirs
- if( !contents[i].isDirectory() )
- {
- fileName = contents[i].getName();
- createDate = new Date(contents[i].lastModified());
- //author =
- CLOB content;
- String cmd = "SELECT * FROM file WHERE filename=fileName";
- ResultSet rest = stmt.executeQuery(cmd);
- CLOB clob = ((OracleResultSet)rset).getCLOB(4);
- System.out.println("the length = " + content[i].length());
- FileInputStream instream = new FileInputStream(content[i]);
- OutputStream outstream = clob.getCharacterOutputStream();
- int size = clob.getBufferSize();
- byte[] buffer = new byte[size];
- int length = -1;
- while ((length = instream.read(buffer)) != -1) {
- outstream.write(buffer, 0, length);
- }
- instream.close();
- outstream.close();}}
复制代码 |
|