在线等:JAVA中如何以大字段(BLOB)存储各类文件(ORACLE数据库)
解决方案 »
- JAVA中查询SQL SERVER表名是变量的语句怎么写呀
- 这次短些。自己实现的JMS远程方法调用。
- Class classType=Class.forName("java.lang.Stirng");和Class classType=java.lang.String有什么不同?
- 问一个正规表达式中的Matcher的问题
- 简单的数据源问题...答出者得分
- 关于ResultSet自动关闭
- 如何把string转换成int?在线等候
- JAR打包成功,也可以运行,但效果却不一样,为什么?
- 为何出java.lang.NoSuchMethodError?
- 选择城市中总是添加不到定义的那些城市 ,哪里错了?求指点
- 如何在JSP页面中禁用回退键?
- 我的JDBC问题
写入:
// 准备数据流
if (pItem.getPointDrawType() != null) {
try {
// 将对象转为二进制流保存
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
// 输出每个对象
oos.writeObject(pItem.getPointDrawType()); byte[] bb = bos.toByteArray();
stmType.setBytes(3, bb);
} catch (Exception e) {
System.out
.println("无法保存坐标到数据库!\t代码:PointTypeItemManager.addTypeItem()\t错误内容:"
+ e); stmType.setBytes(3,null);
}
} else {
stmType.setBytes(3,null);
}读取:
byte[] typeByte = rsType.getBytes("TypeMapType");
if (!rsType.wasNull()) {
try {
// 从数据流构造对象
ByteArrayInputStream bis = new ByteArrayInputStream(
typeByte);
ObjectInputStream ois = new ObjectInputStream(bis); // 读取对象
PointDrawType pdt = (PointDrawType) ois.readObject();
pst.setPointDrawType(pdt);
} catch (Exception ie) {
System.out
.println("获取数据时出错!\t代码:PointManager.getPointTypeItem()\t错误内容:"
+ ie);
}
}
//request.getCharacterEncoding("gb2312");
String content = "";
InitialContext ctx=new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/ccc");
Connection con = ds.getConnection();
String sql="select photoStream,content from Newspaper_Photo where id =1";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql); try {
while(rs.next()) {
content =rs.getString("content");
//rs.setContentType("image/jpg");
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
in.read(b);
}
sout.flush();
sout.close();
sout =null;
}
rs.close();
stmt.close();
con.close();
rs =null;
stmt =null;
con =null; }
catch(Exception e){
System.out.println(e);
}
//out.println(sql1);
%>
sout =null;
}
public Guoyf setBlob(Guoyf guoyf, String file)//实现了2进制文件存入数据库.
{
try{
session = sf.openSession();
guoyf.setB(Hibernate.createBlob(new byte[1])); //---------------------B需要改为相应的BLOB键名
transaction = session.beginTransaction();
session.save(guoyf);
session.flush();
session.refresh(guoyf, LockMode.UPGRADE);
BLOB blob = (BLOB)guoyf.getB(); //---------------------B需要改为相应的BLOB键名
OutputStream out = blob.getBinaryOutputStream();
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[10240];
int len;
while((len=fis.read(buf))>0)
{
out.write(buf, 0, len);
}
fis.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
return guoyf;
}
insert into table values(id, blob_col) values('XXX', empty_blob());
然后ResultSet.getBlob("blob_col");
在然后用流的形式向Blob对象写入数据,最后update这一条记录。