阅读更多
当数据库字段为blob类型时 ,我们如果使用PreparedStatement中的setBinaryStream(int,InputStream,int)方法需要注意
在向blob字段类型中插入数据时,要使用javaio的inputstream,读入文件。
而相反从blob字段中读出数据时,同样使用javaio的inputstream,再用javaio的outputstream写入文件。
同clob的示例中的问题
如果在设置字节流的地方不加类型转换的话,如下:
stat.setBinaryStream(1, in, file.length());
则会出现如下错误
Exception in thread "main" java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
at test.jdbc.BlobRW.create(BlobRW.java:38)
at test.jdbc.BlobRW.main(BlobRW.java:24)
后来看了看java和mysql的jdbc驱动两方面的代码,原因明白,原来是用的jdk1.6的版本中,有长度为long类型的方法。
而对应的mysql的jdbc驱动jar中,还没有实现。
将其进行类型转换后,即可正常运行。
转:http://blog.csdn.net/darkhorsefly/article/details/6024883
package com.allan;
import java.sql.*;
import java.io.*;
public class Storeblobfile {
public static void main(String[] args) {
try{
FileInputStream file = new FileInputStream("C:\\shanshui.jpg");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root");
PreparedStatement ps = conn.prepareStatement("insert into user values(?,?,?)");
ps.setString(1,"blob");
ps.setInt(2,23);
ps.setBinaryStream(3, file, file.available());
ps.executeUpdate();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select file from user where name = 'blob'");
while(rs.next()){
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();
FileOutputStream fout = new FileOutputStream("C:\\copy.jpg");
int b = -1;
while((b=in.read())!=-1){
fout.write(b);
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
转:http://blog.sina.com.cn/s/blog_4b4d59600100boh6.html
hibernate操作mysql的blob数据
package hibernate;
import java.sql.Blob;
public class Blobtest implements java.io.Serializable {
private long id;
private Blob image;
public Blobtest() {
}
public Blobtest(Blob image) {
this.image = image;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public Blob getImage() {
return this.image;
}
public void setImage(Blob image) {
this.image = image;
}
}
测试写入数据:
package hibernate;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream("F:/lzx.jpg");
byte[] b = new byte[in.available()];
in.read(b);
in.close();
Blobtest blobTest=new Blobtest();
blobTest.setImage(Hibernate.createBlob(b));
Session session=HibernateUtil.currentSession();
Transaction tx=session.beginTransaction();
session.save(blobTest);
tx.commit();
System.out.print("success");
HibernateUtil.closeSession();
}
}
读出数据:
package hibernate;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class TestWrite {
public static void main(String[] args) throws IOException, SQLException {
Session session=HibernateUtil.currentSession();
Transaction tx=session.beginTransaction();
Blobtest blobTest = new Blobtest();
blobTest = (Blobtest) session.get(Blobtest.class, new Long(1));
System.out.println("qu chu shu ju");
OutputStream out = new FileOutputStream("F:/111.jpg");
out.write(blobTest.getImage().getBytes(1,(int) blobTest.getImage().length()));
out.flush();
out.close();
HibernateUtil.closeSession();
}
}
转:http://www.blogjava.net/dihin/archive/2006/06/25/54956.html
- 大小: 57.7 KB