本文目录一览:
- 使用java语言操作,如何来实现MySQL中Blob字段的存取
- blob字段java如何处理
- java中blob类型是什么类型
- java 字符串如何转换流存入blob字段中
- java里怎么判断Blob类型是否为空
- java blob
使用java语言操作,如何来实现MySQL中Blob字段的存取
/**
* Title: BlobPros.java
* Project: test
* Description: 把图片存入mysql中的blob字段,并取出
* Call Module: mtools数据库中的tmp表
* File: C:downloadsluozsh.jpg
* Copyright: Copyright (c) 2003-2003
* Company: uniware
* Create Date: 2002.12.5
* @Author: ChenQH
* @version 1.0 版本*
*
* Revision history
* Name Date Description
* ---- ---- -----------
* Chenqh 2003.12.5 对图片进行存取
*
* note: 要把数据库中的Blob字段设为longblob
*/
//package com.uniware;
import java.io.*;
import java.util.*;
import java.sql.*;
public class BlobPros
{
private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=windpassword=123useUnicode=true";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;
public BlobPros()
{
}
/**
* 向数据库中插入一个新的BLOB对象(图片)
* @param infile 要输入的数据文件
* @throws java.lang.Exception
*/
public void blobInsert(String infile) throws Exception
{
FileInputStream fis = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);
file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");
pstmt.setString(1,file.getName()); //把传过来的第一个参数设为文件名
//pstmt.setBinaryStream(2,fis,(int)file.length()); //这种方法原理上会丢数据,因为file.length()返回的是long型
pstmt.setBinaryStream(2,fis,fis.available()); //第二个参数为文件的内容
pstmt.executeUpdate();
}
catch(Exception ex)
{
System.out.println("[blobInsert error : ]" + ex.toString());
}
finally
{
//关闭所打开的对像//
pstmt.close();
fis.close();
conn.close();
}
}
/**
* 从数据库中读出BLOB对象
* @param outfile 输出的数据文件
* @param picID 要取的图片在数据库中的ID
* @throws java.lang.Exception
*/
public void blobRead(String outfile,int picID) throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select pic from tmp where id=?");
pstmt.setInt(1,picID); //传入要取的图片的ID
rs = pstmt.executeQuery();
rs.next();
file = new File(outfile);
if(!file.exists())
{
file.createNewFile(); //如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("pic");
int size = 0;
/* while(size != -1)
{
size = is.read(Buffer); //从数据库中一段一段的读出数据
//System.out.println(size);
if(size != -1) //-1表示读到了文件末
fos.write(Buffer,0,size);
} */
while((size = is.read(Buffer)) != -1)
{
//System.out.println(size);
fos.write(Buffer,0,size);
}
}
catch(Exception e)
{
System.out.println("[OutPutFile error : ]" + e.getMessage());
}
finally
{
//关闭用到的资源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}
public static void main(String[] args)
{
try
{
BlobPros blob = new BlobPros();
//blob.blobInsert("C:Downloadsluozsh1.jpg");
blob.blobRead("c:/downloads/1.jpg",47);
}
catch(Exception e)
{
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}
blob字段java如何处理
- 使用jdk中的方法进行传输。在ResultSet 中有getBlob()方法,在PreparedStatement中有setBlob()方法,所以大多数人都会尝试setBlob(),getBlob() 进行读写,或者两个数据库之间BLOB的传输。这种方法实际上是行不通的,据网上的一些资料介绍,说sun官方的文档有些方法都是错误的。
- 使用ResultSet.getBinaryStream 和PreparedStatement.setBinaryStream对BLOB进行读写或两个数据库间的传输。这种方法我自己尝试过,发现,如果BLOB中存储的是文本文件的话,就没问题,如果是二进制文件,传输就会有问题。 根据自己的经验,以及查阅了Oracle的官方文档,都是使用如下处理方法:
- 新建记录,插入BLOB数据
1.1 首先新建记录的时候,使用oracle的函数插入一个空的BLOB,假设字段A是BLOB类型的:
1.2 后面再查询刚才插入的记录,然后更新BLOB,在查询前,注意设置Connection的一个属性:insert xxxtable(A,B,C) values(empty_blob(),'xxx','yyyy')
如果缺少这一步,可能导致fetch out of sequence等异常. 1.3 查询刚才插入的记录,后面要加“ for update ”,如下:conn.setAutoCommit(false);
如果缺少for update,可能出现row containing the LOB value is not locked的异常 1.4 从查询到的 BLOB字段中,获取blob并进行更新,代码如下:select A from xxxtable where xxx=999 for update
后面再使用output.write方法将需要写入的内容写到output中就可以了。例如我们将一个文件写入这个字段中:BLOB blob = (BLOB) rs.getBlob("A"); OutputStream os = blob.getBinaryOutputStream(); BufferedOutputStream output = new BufferedOutputStream(os);
上面的代码就是从input里2k地读取,然后写入到output中。 1.5 上面执行完毕后,记得关闭output,input,以及关闭查询到的ResultSet 1.6 最后执行conn.commit();将更新的内容提交,以及执行conn.setAutoCommit(true); 改回Connction的属性BufferedInputStream input = new BufferedInputStream(new File("c://hpWave.log").toURL().openStream()); byte[] buff = new byte[2048]; //用做文件写入的缓冲 int bytesRead; while(-1 != (bytesRead = input.read(buff, 0, buff.length))) { output.write(buff, 0, bytesRead); System.out.println(bytesRead); }
- 修改记录,方法与上面的方法类似, 2.1 首先更新BLOB以外的其他字段 2.2 使用1.3中类似的方法获取记录 2.3 修改的过程中,注意以下: a. 需要更新的记录中,BLOB有可能为NULL,这样在执行blob.getBinaryOutputStream()获取的值可能为null,那么就关闭刚才select的记录,再执行一次update xxxtable set A = empty_blob() where xxx, 这样就先写入了一个空的BLOB(不是null),然后再使用1.3,1.4中的方法执行更新记录. b. 注意别忘了先执行setAutoCommit(false),以及 "for update",以及后面的conn.commit();等。
- 读取BLOB字段中的数据.
3.1 读取记录不需要setAutoCommit(),以及 select ....for update.
3.2 使用普通的select 方法查询出记录
3.3 从ResultSet中获取BLOB并读取,如下:
通过循环取出blob中的数据,写到buff里,再将buff的内容写入到需要的地方BLOB b_to = (BLOB) rs.getBlob("A"); InputStream is = b_from.getBinaryStream(); BufferedInputStream input = new BufferedInputStream(is); byte[] buff = new byte[2048]; while(-1 != (bytesRead = input.read(buff, 0, buff.length))) { //在这里执行写入,如写入到文件的BufferedOutputStream里 System.out.println(bytesRead); }
- 两个数据库间blob字段的传输 类似上面1和3的方法,一边获取BufferedOutputStream,另外一边获取BufferedInputStream,然后读出写入,需要注意的是写入所用的Connection要执行conn.setAutoCommit(false);以及获取记录时添加“ for update ”以及最后的commit(); 总结以上方法,其根本就是先创建空的BLOB,再获取其BufferedOutputStream进行写入,或获取BufferedInputStream进行读取 (1)对数据库clob型执行插入操作
java.sql.PreparedStatement pstmt = null;
ResultSet rs = null;
String query = "";
conn.setAutoCommit(false);
query = "insert into clobtest_table(id,picstr) values(?,empty_clob())";
java.sql.PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1,"001");
pstmt.executeUpdate();
pstmt = null;
query = "select picstr from clobtest_table where id = '001' for update";
pstmt = con.prepareStatement(query);
rs= pstmt.executeQuery();
oracle.sql.CLOB clobtt = null;
if(rs.next()){
clobtt = (oracle.sql.CLOB)rs.getClob(1);
}
Writer wr = clobtt.getCharacterOutputStream();
wr.write(strtmp);
wr.flush();
wr.close();
rs.close();
con.commit();
(2)通过sql/plus查询是否已经成功插入数据库 PL/SQL的包DBMS_LOB来处理LOB数据。察看刚才的插入是否成功。使用DBMS_LOB包的getlength这个procedure来检测是否已经将str存入到picstr字段中了。如:
select dbms_lob.getlength(picstr) from clobtest_table;
(3)对数据库clob型执行读取操作 读取相对插入就很简单了。基本步骤和一半的取数据库数据没有太大的差别。
String description = "";
query = "select picstr from clobtest_table where id = '001'";
pstmt = con.prepareStatement(query);
ResultSet result = pstmt.executeQuery();
if(result.next()){
oracle.jdbc.driver.OracleResultSet ors = (oracle.jdbc.driver.OracleResultSet)result;
oracle.sql.CLOB clobtmp = (oracle.sql.CLOB) ors.getClob(1);
if(clobtmp==null || clobtmp.length()==0){
System.out.println("======CLOB对象为空 ");
description = "";
}else{
description=clobtmp.getSubString((long)1,(int)clobtmp.length());
System.out.println("======字符串形式 "+description);
}
}
java中blob类型是什么类型
blob是数据库二进制对象的类型,图片,文本之类的。 java没有特定类,非要说的话,就是个超大的字节数组~
java 字符串如何转换流存入blob字段中
将字符串转换成byte数组String.getBytes()
,然后放进一个ByteArrayInputStream输入流中即可存入BLOB字段中
PreparedStatement.setBlob(int parameterIndex, InputStream inputStream)
java里怎么判断Blob类型是否为空
要看写的Blob类型是什么 是不是只的boolean类型还是声明为Boolean的类 jdk1.4情况下 java里有基本类型的boolean和Boolean的包装类。两者是有区别的。至于什么区别,我想楼主应该多看看书。 Blooean的声明的变量是引用类型。这个引用将指向一个对象。该对象可以为空。例如:
Boolean b = null;
System.out.println(b);
打印的结果是 null; 当然如果声明为基本类型,打印结果默认是false的,例如:
boolean b;
System.out.println(b);
打印的结果是false jdk5.0的情况没做测试结果不清楚。估计结果和上面是一样的。 但是因为在jdk5.0的情况下包装类对象和基本类型似乎可以互相直接转化例如:
int i = new Integer(5);
这种语句在1.4情况下编译不过的,似乎5.0就可以。所以5.0以后的情况不确定
java blob
java blob是什么,让我们一起了解一下? Blob是计算机视觉图像中的一块连通区域,Blob分析的就是对前景或背景分离后的二值图像,进行连通域提取和标记以及计算Blob的一些相关特征,而且通过Blob提取,还可以获得相关区域的信息。 Blob分析的重要一个步骤是连通区域的确定,那么它的优缺点是什么? 优点: Blob在目标跟踪的优势有:
- 通过Blob提取,可以获得相关区域的信息,这些信息可以作为边缘监测器或者角点检测器的补充信息。在目标识别中,Blob可以提供局部的统计信息和外貌信息,这些信息能够为目标识别和跟踪提供依据。
- 可以利用Blob对直方图进行峰值检测。
- Blob还可以作为纹理分析和纹理识别的基元。
- 通过Blob分析,可以得到目标的个数及其所在区域,在进行目标匹配时,不需要对全局图像进行搜索。 缺点:
- 速度过慢,要整个区域作逐点扫描。
- Blob分析难度大。这是一纯几何学上的问题,一个不规则的形状,如何计算它的面积、大小没有简单易行的算法,太过复杂,运算时间就长,速度就更慢了。
- 实际应用。Blob算法在实际应用中,非常依赖光源。几乎可以说,Blob算法如果离开了一个可靠的光源设计,则完全不起作用。 那么java是怎样对Blob读写的?示例如下:
package com.you.sister;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class BlobTest {
public static Connection conn;
public static Connection getConn() throws Exception {
FileInputStream fis = new FileInputStream(new File("jdbc.properties"));
Properties prop = new Properties();
prop.load(fis);
String driver = prop.getProperty("jdbc.driver");
String url = prop.getProperty("jdbc.url");
String username = prop.getProperty("jdbc.username");
String password = prop.getProperty("jdbc.password");
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public static void main(String[] args) throws Exception {
conn = getConn();
readBlob();
writeBlob();
conn.close();
}
/**
* 从数据库中读大对象出来
* 保存在本地
*/
public static void readBlob() {
try {
String readSql = "select * from emp where empno = ?";
PreparedStatement ps = conn.prepareStatement(readSql);
ps.setInt(1, 7369);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Blob image = rs.getBlob("image");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(7369 + "_image.jpeg"));
InputStream fis = image.getBinaryStream();
int out;
byte[] outByte = new byte[100];
while ((out = fis.read(outByte)) != -1) {
dos.write(outByte);
}
fis.close();
dos.flush();
dos.close();
}
rs.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将大对象文件保存进数据库中
*/
public static void writeBlob() {
try {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(new File("D:\\Tulips.jpg")));
String writeSql = "select * from emp where empno = ? for update";
PreparedStatement ps = conn.prepareStatement(writeSql);
ps.setInt(1, 7499);
conn.setAutoCommit(false);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
oracle.sql.BLOB image = (oracle.sql.BLOB) rs.getBlob("image");
BufferedOutputStream bos = new BufferedOutputStream(image.getBinaryOutputStream());
int c;
while ((c = fis.read()) != -1) {
bos.write(c);
}
fis.close();
bos.close();
}
conn.commit();
rs.close();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}