`

jdbc操作blob

    博客分类:
  • jdbc
 
阅读更多

package demo;
import java.sql.*;
import java.io.*;
import java.sql.PreparedStatement;
import java.util.*;

import oracle.jdbc.driver.*;
import oracle.sql.BLOB;


/**
* Insert record in the MEDIA table
* MEDIA (file_name varchar2(256), file_content BLOB);
*/
public class BlobOracle
{
private final static String hostname = "localhost";
private final static String port = "1521";
private final static String sid = "ORCL";
private final static String username = "scott";
private final static String password = "tiger";
private static String fileLocation;
private static Connection connection;

public BlobOracle()
{
}

/**
*
* @param args
*/
public static void main(String[] args)
{
try
{
if (args.length == 0)
{
System.out.println("\n\n Usage demo.BlobOracle ");
System.exit(0);
}

fileLocation = args[0];

setConnection();
insertBLOB();

} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
}
}


private static void setConnection() throws SQLException
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connection = DriverManager.getConnection("jdbc:oracle:thin:@"+hostname+ ":"+ port +":"+ sid , username , password);
connection.setAutoCommit(false); // we must control the commit
}

private static void insertBLOB() throws SQLException, Exception
{
BLOB blob;
File file ;
FileInputStream is;
OutputStream os;

long ts1 = System.currentTimeMillis();


//Create a statement.
PreparedStatement pstmt = connection.prepareStatement("insert into media (file_name, file_content) values (?,empty_blob())");
file = new File(fileLocation);
String fileName = file.getName();
//Set the file name and execute the query
pstmt.setString(1, fileName);
pstmt.execute();

//Take back the record for update (we will insert the blob)
//supposely the file name is the PK
pstmt = connection.prepareStatement("select file_content from media where file_name = ? for update");
pstmt.setString(1, fileName);

//Execute the query, and we must have one record so take it
ResultSet rset = pstmt.executeQuery();
rset.next();

//Use the OracleDriver resultset, we take the blob locator
blob = ((OracleResultSet)rset).getBLOB("file_content");

is = new FileInputStream(file); //Create a stream from the file
// JDBC 2.0
//os = blob.getBinaryOutputStream(); //get the output stream from the Blob to insert it
// JDBC 3.0
os = blob.setBinaryStream(0); //get the output stream from the Blob to insert it

//Read the file by chuncks and insert them in the Blob. The chunk size come from the blob
byte[] chunk = new byte[blob.getChunkSize()];
int i=-1;
System.out.println("Inserting the Blob");
while((i = is.read(chunk))!=-1)
{
os.write(chunk,0,i); //Write the chunk
System.out.print('.'); // print progression
}

// When done close the streams
is.close();
os.close();

//Close the statement and commit
pstmt.close();
long ts2 = System.currentTimeMillis();

connection.commit();
connection.close();

System.out.println("\n"+ (ts2 - ts1) +" ms" );


}


}
 
分享到:
评论

相关推荐

    jdbc 操作oracle blob数据

    jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc 操作oracle blob数据jdbc ...

    上传图片,保存到数据库,jdbc操作blob

    上传图片,保存到数据库,jdbc操作blob

    JDBC中操作Blob、Clob等对象 实例详细 非常详细

    JDBC中操作Blob、Clob等对象 实例详细JDBC中操作Blob、Clob等对象 实例详细JDBC中操作Blob、Clob等对象 实例详细JDBC中操作Blob、Clob等对象 实例详细JDBC中操作Blob、Clob等对象 实例详细JDBC中操作Blob、Clob等...

    JDBC中操作Blob、Clob等对象

    JDBC中操作Blob、Clob等对象

    使用JDBC4.0操作Oracle中BLOB类型的数据方法

    主要介绍了使用JDBC4.0操作Oracle中BLOB类型数据的方法,我们需要使用ojdbc6.jar包,本文介绍的非常详细,需要的朋友可以参考下

    使用Jdbc4操作Blob,Clob

    NULL 博文链接:https://53873039oycg.iteye.com/blog/2009996

    详解jdbc实现对CLOB和BLOB数据类型的操作

    主要介绍了详解jdbc实现对CLOB和BLOB数据类型的操作的相关资料,这里实现写入操作与读写操作,需要的朋友可以参考下

    利用spring的jdbcTemplate处理blob、clob

    spring 中对大数据的处理,包括clob,blob的数据。比之jdbc下简便很多。

    Java-图片BLOB的存取DEMO

    20180828开发,提供了三种通过JDBC实现图片存储到BLOB的方式: 1、com.ztf:实现小页面,实现 指定保存的图片+打开指定编号图片 2、com.ub:实现简单的插入及读取操作,读取后的BLOB可以生成新图片,并使用JFrame...

    jdbc基础(概念、操作步骤、连接方式等)

    jdbc基础,包含基本概念、数据库连接操作、JDBC常用接口、与oracle/mysql/db2创建连接、Statement、PreparedStatement会话使用、Clob/Blob大文件处理、批处理操作等。

    关于在Hibernate中对于Clob,Blob字段的处理方法

    oracle的jdbc驱动程序,用这个版本的操作Clob,blob类型的数据很方便。 博文链接:https://zhenjw.iteye.com/blog/173419

    使用JDBC4.0处理Oracle中BLOB类型的数据

    需要的jar包  使用ojdbc6.jar  在/META-INF/MANIFEST.MF里可以看到Specification-Version: 4.0  建表 ... image blob );  将文件写入数据库 1 /** 2 * 将图片文件存入数据库 3 * @throws

    jdbc批量插入大字段

    针对oracle中blob字段的操作,能批量快速的插入大字段,效率非常高

    你不知道的JDBC高级应用

    3.特殊类型(date--日期,clob--大字段,blob--二进制) 的操作. 4.元数据(MetaData)的操作:包括 parameter , DB , ResultSet的元数据,配合反射,大幅度提升程序的灵活性. 5.储存过程和CRUD的操作,获得Statement可以添加...

    jdbc连接数据库的方式2

    二、JDBC连接MySql方式 下面是使用JDBC连接MySql的一个小的教程 1、查找驱动程序 MySQL目前提供的java驱动程序为Connection/J,可以从MySQL官方网站下载,并找到mysql-connector-java-3.0.15-ga-bin.jar文件,此...

    Java_JDBC由浅入深

    4.2 CRUD操作 16 4.3 Statement有那些缺点 19 第五节 ResultSet接口的使用详解 20 第六节 JDBC 中数据类型详解 30 6.1 基本数据类型 30 6.2 日期类型 34 6.3 CLOB类型 36 6.4 BLOB类型 39 6.5 其他数据类型 41 第七...

    java高手的文章合集3/3

    JDBC中操作Blob、Clob等对象.pdf JDBC中驱动加载的过程分析(上).pdf JDBC中驱动加载的过程分析(下).pdf 从大学教育与工作的差距谈源代码阅读的必要.pdf 给学习J2EE的朋友一些值得研究的开源项目.pdf 教你建立...

Global site tag (gtag.js) - Google Analytics