java blob/clob

//2012-03-18 针对oracle 数据库中大对象的读写 getBinaryOutPutStream()

import java.sql.
*;
import java.io.
*;
//声明clob/blob必须要导入oracle.sql.*
import oracle.sql.
*;
public class jdbc_blob {

  
public static void main (String args[]) throws SQLException,IOException {

    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (Exception e) {
      System.out.println("异常"
+e.getMessage());
      System.
exit(1);
    }

    Connection conn 
= null;
    Statement stmt 
= null;
    ResultSet rset 
= null;
    ResultSet rs 
= null;
    
//定义文件存放
    String sourceDirectory
="d:\\javasource\\db\\";
    
//String filename=sourceDirectory+"textecontent.txt";
    try {
      conn 
= DriverManager.getConnection(
          "jdbc:oracle:thin:
@192.168.1.102:1521:ccxe", "scott", "test");
        
//diable auto commit;
        conn.setAutoCommit(false);

        
//建立statement 对象
        stmt
=conn.createStatement();
        writeCLOB(stmt,sourceDirectory
+"textcontent.txt");
        writeBLOB(stmt,sourceDirectory
+"Java_jdbc数据库连接池总结.doc");
        
//addBFILE(stmt,"SAMPLE_FILE_DIR","textcontent.txt");
        
//addBFILE(stmt,"SAMPLE_FILE_DIR","Java_jdbc数据库连接池总结.doc");
        stmt.
close();
        conn.
close(); //关闭数据库连接
      } catch (SQLException e)
          {
            System.out.println("Darn! A SQL error: " 
+ e.getMessage());
         } 
  }
//定义方法。
private static void writeCLOB(Statement stmt,String filename) throws SQLException,IOException
    {
    stmt.executeUpdate("
insert into clob_content(file_name,clob_column)"+values ('"+filename+"',EMPTY_CLOB())");
    ResultSet clobResultSet
=stmt.executeQuery("select clob_column from clob_content ");
    
if (    clobResultSet.next()){
    Clob myClob
= clobResultSet.getClob("clob_column");
BufferedReader myBufferedReader 
= new BufferedReader( new InputStreamReader(new FileInputStream(filename)));
PrintWriter out 
= new PrintWriter(new BufferedWriter(((oracle.sql.CLOB) myClob).setCharacterStream(0)));
String 
str=null;
System.out.println("开始写");
while ((str=myBufferedReader.readLine())!=null)
{
    
//myClob.putChars(position,textBuffer);
    out.println(
str);
    System.out.println(
str);
    
//position+=charsRead;
}
out.
close();
myBufferedReader.
close();
stmt.
execute("commit");
    }
//end if
System.out.println("write content 
from file+ filename+to clob");
}
//end of writeCLOB;

private static void writeBLOB(Statement stmt,String filename) throws SQLException,IOException
{
    stmt.executeUpdate("
insert into blob_content (file_name,blob_column)"+values (' "+filename +"', EMPTY_BLOB())");
    System.out.println(filename);
    ResultSet blobResultSet
=stmt.executeQuery("select blob_column from blob_content ");
    System.out.println(blobResultSet.
next());
    
if(blobResultSet.next()){  
    Blob myBlob
=blobResultSet.getBlob("blob_column");
    OutputStream out
=myBlob.setBinaryStream(0);
    
int chunksize=((oracle.sql.BLOB)myBlob).getBufferSize();
    byte
[] byteBuffer=new byte[chunksize];
    
//FileInputStream myFileInputStream=new  FileInputStream(filename);
    BufferedInputStream 
in = new BufferedInputStream(new FileInputStream(filename), chunksize);
    
int count = in.read(byteBuffer, 0, chunksize);
    
int bytesRead=0;
    System.out.println(
count);
        
while(count!=-1)
    {
    out.write(byteBuffer,
0count);
    bytesRead
+=count;
     System.out.println("处理了 " 
+ bytesRead + " 字节");
     
count = in.read(byteBuffer, 0, chunksize);
     System.out.println("处理了 " 
+ bytesRead + " 字节,成功");

    
//    myBlob.putBytes(position,byteBuffer);
    
//    position+=bytesRead;
    }
        out.
close();
    
in.close();
    stmt.
execute("commit");

    }
    System.out.println("write content 
from file"+filename+"to blob");
}
// end of writeBLOB();

private static void addBFILE(Statement stmt,String directory,String filename) throws SQLException{
    String sql;
    sql
="insert into bfile_content(file_name,bfile_column)"+values ('"+filename +"',"+"BFILENAME('"+directory+"','"+filename+"'))";
    System.out.println(sql);
    stmt.executeUpdate (sql);
    stmt.
execute("commit");
    System.out.println("added pointer 
to file "+filename+to BFILE in database directory  "+directory);
}
// end of addbfile();
}


当然。前期你得准备相关表和文件

posted on 2012-03-20 00:12 snowhill 阅读(243) 评论(0)  编辑 收藏 引用 所属分类: java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

公告

又一年...........

留言簿(3)

随笔分类(13)

文章分类(131)

文章档案(124)

c++

java

linux

oracle

常用软件

其他

网络配置

系统安全

音乐

搜索

最新评论

阅读排行榜