`
maosheng
  • 浏览: 551435 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java读写Oracle数据库blob

    博客分类:
  • Java
阅读更多
Java写音频文件到Oracle数据库:

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;
}


Java读音频文件从Oracle数据库:

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;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics