时间:2021-07-01 10:21:17 帮助过:7人阅读
/*
* 文件名:BlobTest.java
* 版权:Copyright by www.huawei.com
* 描述:
* 修改人:Cuigaochong
* 修改时间:2015-8-25
* 跟踪单号:
* 修改单号:
* 修改内容:
*/
package com.jdbc.cgc.blob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.sql.BLOB;
import org.junit.Test;
/**
* <一句话功能简述> <功能详细描述>
*
* @author 姓名 工号
* @version [版本号, 2015-8-25]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class BlobTest
{
/**
* <一句话功能简述>测试方法,插入一条数据,数据中有一个列是Blob <功能详细描述>
*
* @throws FileNotFoundException
* @see [类、类#方法、类#成员]
*/
@Test
public void test00()
throws FileNotFoundException
{
// 注意: 对于empty_blob()应放在SQL语句中直接赋值, 使用预置语句的方式赋值无法实现.
String sql = "insert into t_emp13(first_name,salary,picture) values (?,?,Empty_BLOB())";
// 查询Blob 注意进行 行锁定 后边的for update
String sqlQuery = "select picture from t_emp13 where first_name = ? and salary = ? for update";
updateBlob(sql, sqlQuery, "QQA122", 1233);
}
/**
* <一句话功能简述>更新有blob的数据库表 <功能详细描述>
*
* @param sqlInsert
* @param sqlQuery
* @param args
* @see [类、类#方法、类#成员]
*/
public void updateBlob(String sqlInsert, String sqlQuery, Object... args)
{
Connection conn = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
OutputStream os = null;
FileInputStream fis = null;
try
{
// 现在表中插入空的Blob
conn = getConn();
// 事物处理前,取消Connection的默认提交行为
conn.setAutoCommit(false);
prepareStatement = conn.prepareStatement(sqlInsert);
for (int i = 0; i < args.length; i++)
{
prepareStatement.setObject(i + 1, args[i]);
}
prepareStatement.executeUpdate();
BLOB blob = null;
prepareStatement = conn.prepareStatement(sqlQuery);
for (int i = 0; i < args.length; i++)
{
prepareStatement.setObject(i + 1, args[i]);
}
// prepareStatement.setString(1, "QQA");
rs = prepareStatement.executeQuery();
if (rs.next())
{
blob = (BLOB)rs.getBlob(1);
}
// 得到数据库的输出流
os = blob.getBinaryOutputStream();
// 得到要插入文件的输入流
fis = new FileInputStream("Tulips.jpg");
byte[] b = new byte[1024];
int len;
while (-1 != (len = fis.read(b)))
{
os.write(b, 0, len);
}
// 清空流的缓存
os.flush();
// 事物处理:如果事物处理成功则提交事物
conn.commit();
}
catch (Exception e)
{
e.printStackTrace();
try
{
// 事物处理:如果出现异常 则在catch块中回滚事物
conn.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
finally
{
if (null != fis)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (null != os)
{
try
{
os.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
releaseSource(prepareStatement, conn, null);
}
}
/**
* <一句话功能简述>测试方法,查询有blob的表 <功能详细描述>
*
* @see [类、类#方法、类#成员]
*/
@Test
public void test01()
{
String sql = "select picture from t_emp13 where first_name = ?";
queryBlob(sql, "QQA122");
}
/**
* <一句话功能简述>查询有Blob的表 方法 <功能详细描述>
*
* @param sql
* @param args
* @see [类、类#方法、类#成员]
*/
public void queryBlob(String sql, Object... args)
{
Connection conn = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
FileOutputStream fos = null;
InputStream is = null;
try
{
conn = getConn();
// 事物处理前,取消Connection的默认提交行为
conn.setAutoCommit(false);
prepareStatement = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++)
{
prepareStatement.setObject(i + 1, args[i]);
}
rs = prepareStatement.executeQuery();
if (rs.next())
{
BLOB blob = (BLOB)rs.getBlob(1);
is = blob.getBinaryStream();
fos = new FileOutputStream(new File("test.png"));
byte[] b = new byte[1024];
int len;
while (-1 != (len = is.read(b)))
{
fos.write(b, 0, len);
}
fos.flush();
}
// 事物处理:如果事物处理成功则提交事物
conn.commit();
}
catch (Exception e)
{
e.printStackTrace();
try
{
// 事物处理:如果出现异常 则在catch块中回滚事物
conn.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
finally
{
if (null != is)
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (null != fos)
{
try
{
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
releaseSource(prepareStatement, conn, rs);
}
}
/**
* <一句话功能简述> 连接数据库 <功能详细描述>
*
* @return
* @throws Exception
* @see [类、类#方法、类#成员]
*/
public Connection getConn()
throws Exception
{
String dirverName = null;
String jdbcUrl = null;
String user = null;
String password = null;
Properties propertoes = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
propertoes.load(is);
dirverName = propertoes.getProperty("driver");
jdbcUrl = propertoes.getProperty("jdbcURL");
user = propertoes.getProperty("user");
password = propertoes.getProperty("password");
Class.forName(dirverName);
// 通过DriverManager的getConnection获取数据库连接
Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
return connection;
}
/**
* <一句话功能简述>释放数据库资源 <功能详细描述>
*
* @param statement
* @param conn
* @param resultSet
* @see [类、类#方法、类#成员]
*/
public void releaseSource(Statement statement, Connection conn, ResultSet resultSet)
{
if (null != resultSet)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (null != statement)
{
try
{
statement.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (null != conn)
{
try
{
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
JDBC ORACLE BLOB处理
标签:oracle jdbc java blob 存储