blob存储文件示例程序
来源:互联网 发布:java输出当前系统时间 编辑:程序博客网 时间:2024/05/22 01:38
建表文件
create table A_F2DB
(
TID VARCHAR2(23) not null,
A_FILEC CLOB,
A_FILEB BLOB
)
(
TID VARCHAR2(23) not null,
A_FILEC CLOB,
A_FILEB BLOB
)
WriteBlob.java
package com.fsm.db;
import java.sql.*;
import java.util.ArrayList;
import java.io.*;
import oracle.sql.*;
public class WriteBlob ...{
////////////////////////////-----------icepoint------------////////////////////////////////
/** *//**
* @param strTID
* @param tablename
* @param filenames
* @param blobcolumnname
* @param KeyColumn
* @return
*/
////////////////////////////-----------icepoint------------////////////////////////////////
public boolean writeblobtodb(String strTID, String tablename,
String filenames, String blobcolumnname, String KeyColumn) ...{
boolean isok = false;
try ...{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "gj", "gj");
conn.setAutoCommit(false);
ReadPath readPath = new ReadPath();
ArrayList list = readPath.okpath();
File f = new File(filenames);
FileInputStream fin = new FileInputStream(f);
BLOB blob = null;
PreparedStatement pstmt = conn.prepareStatement("insert into "
+ tablename + " (" + KeyColumn + "," + blobcolumnname
+ ") values(?,empty_blob())");
pstmt.setString(1, strTID);
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement("select " + blobcolumnname + " from "
+ tablename + " where " + KeyColumn + "=? for update");
pstmt.setString(1, strTID);
ResultSet rset = pstmt.executeQuery();
if (rset.next())
blob = (BLOB) rset.getBlob(1);
pstmt = conn.prepareStatement("update " + tablename + " set "
+ blobcolumnname + "=? where " + KeyColumn + "='" + strTID
+ "'");
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new byte[(int) fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
pstmt.setBlob(1, blob);
pstmt.executeUpdate();
pstmt.close();
System.out.println("插入成功!!!");
isok = true;
conn.commit();
conn.close();
} catch (SQLException e) ...{
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) ...{
System.err.println(e.getMessage());
}
return isok;
}
public static void main(String[] args) ...{
WriteBlob wb=new WriteBlob();
String strTID = "2007";
String tablename = "A_F2DB";
String filenames = "E:/chunye.jpg";
String blobcolumnname = "A_FILEB";
String KeyColumn = "TID";
if(wb.writeblobtodb(strTID,tablename,filenames,blobcolumnname,KeyColumn))...{
System.out.println("OK");
}else...{
System.out.println("FAILD");
}
}
}
import java.sql.*;
import java.util.ArrayList;
import java.io.*;
import oracle.sql.*;
public class WriteBlob ...{
////////////////////////////-----------icepoint------------////////////////////////////////
/** *//**
* @param strTID
* @param tablename
* @param filenames
* @param blobcolumnname
* @param KeyColumn
* @return
*/
////////////////////////////-----------icepoint------------////////////////////////////////
public boolean writeblobtodb(String strTID, String tablename,
String filenames, String blobcolumnname, String KeyColumn) ...{
boolean isok = false;
try ...{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "gj", "gj");
conn.setAutoCommit(false);
ReadPath readPath = new ReadPath();
ArrayList list = readPath.okpath();
File f = new File(filenames);
FileInputStream fin = new FileInputStream(f);
BLOB blob = null;
PreparedStatement pstmt = conn.prepareStatement("insert into "
+ tablename + " (" + KeyColumn + "," + blobcolumnname
+ ") values(?,empty_blob())");
pstmt.setString(1, strTID);
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement("select " + blobcolumnname + " from "
+ tablename + " where " + KeyColumn + "=? for update");
pstmt.setString(1, strTID);
ResultSet rset = pstmt.executeQuery();
if (rset.next())
blob = (BLOB) rset.getBlob(1);
pstmt = conn.prepareStatement("update " + tablename + " set "
+ blobcolumnname + "=? where " + KeyColumn + "='" + strTID
+ "'");
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new byte[(int) fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
pstmt.setBlob(1, blob);
pstmt.executeUpdate();
pstmt.close();
System.out.println("插入成功!!!");
isok = true;
conn.commit();
conn.close();
} catch (SQLException e) ...{
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) ...{
System.err.println(e.getMessage());
}
return isok;
}
public static void main(String[] args) ...{
WriteBlob wb=new WriteBlob();
String strTID = "2007";
String tablename = "A_F2DB";
String filenames = "E:/chunye.jpg";
String blobcolumnname = "A_FILEB";
String KeyColumn = "TID";
if(wb.writeblobtodb(strTID,tablename,filenames,blobcolumnname,KeyColumn))...{
System.out.println("OK");
}else...{
System.out.println("FAILD");
}
}
}
- blob存储文件示例程序
- mysql blob字段存储图片操作示例
- php 存储文件到oracle blob 字段
- SQL Server:存储图像和BLOB文件
- sqlite3中BLOB数据类型存储大对象运用示例
- sqlite3中BLOB数据类型存储大对象运用示例
- sqlite3中BLOB数据类型存储大对象运用示例
- sqlite3中BLOB数据类型存储大对象运用示例
- mongodb 存储文件示例
- blob存储
- 存储blob
- android内部文件存储示例
- 通过oracle存储过程的Blob参数上传文件
- 使用存储过程向Oracle数据库中插入BLOB文件
- T_SQL存储过程读写图片,文件等(BLOB)字段
- 【转】php 存储文件到oracle blob 字段
- T_SQL存储过程读写图片,文件等(BLOB)字段
- fileupload上传文件存储到oracle Blob字段中
- 条件表达式的语法
- 探索软件设计模式(二)
- 简单文件读写
- SAP相关的网址收集
- 关于统计考勤记录的一段SQL
- blob存储文件示例程序
- AXIS学习笔记
- 读写INI文件
- 系统设计的一些原则(转)
- 如何限制程序只运行一次
- 发现一枚牛人
- web开发框架——JSF开发实战二(图)
- 转载: 愚翁专栏 探讨DataGrid的几种分页方法的优劣
- Word文档的读取与分析