Java读写Oracle数据库blob

[b]Java写音频文件到Oracle数据库:[/b]

public boolean SaveVoiceFileToDb(String filename,String information_number,long sequece_number) {
boolean returnflag=false;
File file = new File(filename);
String insertSQL = "insert into t_vedio(serial_number,information_number,vedio_information) values(?,?,empty_blob())";
String updateSQL = "select vedio_information from t_vedio where information_number=? for update";

BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
BasicDataSource datasource=(BasicDataSource)bf.getBean("datasource");

Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
FileInputStream in = null;
System.out.println("read file " + filename + " length=" + file.length());
try {
if (file.length() > 0) {
Class.forName(datasource.getDriverClassName());
con = DriverManager.getConnection(datasource.getUrl(),datasource.getUsername(),datasource.getPassword());
con.setAutoCommit(false);
ps = con.prepareStatement(insertSQL);
ps.setLong(1, sequece_number);
ps.setString(2, information_number);
// 插入一个空对象empty_blob()
ps.execute();
// 锁定数据行进行更新,注意“for update”语句
System.out.println(updateSQL);

ps=con.prepareStatement(updateSQL);
ps.setString(1, information_number);

rs = ps.executeQuery();
while (rs.next()) {
// 得到java.sql.Blob对象后强制转换为oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("vedio_information");
OutputStream outStream = blob.getBinaryOutputStream();
// data是传入的byte数组,定义:byte[] data
if (!file.exists() || file.isDirectory())
{ throw new FileNotFoundException();
}
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int len=0;
while ((len=fis.read(buf)) != -1) {
outStream.write(buf, 0, len);
outStream.flush();
buf = new byte[1024];// 重新生成,避免和上次读取的数据重复
}
outStream.close();
}
con.commit();
rs.close();
} else {
System.out.println("read file " + filename + " error");
}

returnflag=true;

} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null)
try { in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

return returnflag;
}


[b]Java读音频文件从Oracle数据库:[/b]

public byte[] getVedioInfoByte(String information_number) {
byte[] buffer = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = C3P0Helper.createInstance().getConnection();

String sql = "select v.vedio_information from t_vedio v where v.information_number=?";

stmt = conn.prepareStatement(sql);
stmt.setString(1, information_number);

stmt.execute();

rs = stmt.getResultSet();
// String filepath = "F:\\test_result.mp3";
// System.out.println("输出文件路径为:" + filepath);
while (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
if (blob != null && !blob.isEmptyLob()) {
InputStream in = blob.getBinaryStream(); // 建立输出流
// FileOutputStream file = new FileOutputStream(filepath);
int len = (int) blob.length();
buffer = new byte[len]; // 建立缓冲区
while ( (len = in.read(buffer)) != -1) {
// file.write(buffer, 0, len);
break;
}
// file.close();
in.close();
System.out.println(terminal_no + "'s vedio information size:"+len);
}

}

} catch (SQLException ex2) {

ex2.printStackTrace();

} catch (Exception ex2) {

ex2.printStackTrace();

} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}

if (conn != null) {
conn.close();
}

} catch (SQLException ex1) {

}

}

return buffer;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值