jdbc保存图片到数据库并读取代码
2014-05-15 22:22
351 查看
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import oracle.sql.BLOB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import util.DBConnection;
import base.dao.BaseDAO;
public class ImgDAOImpl extends BaseDAO implements ImgDAO {
Connection con;
ResultSet rs;
PreparedStatement pre;
BLOB img;//oracle.sql.BLOB类 不能用java.sql.Blob 操纵clob也同理
boolean isSuccess;
public void save() throws Exception {
con = DBConnection.getDBConnection().getConnection();
con.setAutoCommit(false);
pre = con.prepareStatement("insert into fw.img values (?,empty_blob())");
pre.setInt(1, 1);
if(pre.executeUpdate()>0)
{
pre = con.prepareStatement("select i_img from fw.img where i_id=? for update");
pre.setInt(1, 1);
rs = pre.executeQuery();
if(rs.next())
{
img = (BLOB) rs.getBlob("i_img");//获得blob字段
InputStream is = new FileInputStream("src/1.jpg");
byte[] bt = new byte[is.available()];
is.read(bt);
OutputStream os = img.getBinaryOutputStream();
os.write(bt);
os.flush();
is.close();
os.close();
isSuccess=true;
}
if(isSuccess)
{
con.commit();
System.out.println("插入完毕");
}
else
{
con.rollback();
}
}
else
{
con.rollback();
}
DBConnection.getDBConnection().closeConnection(rs, pre, con);
}
public void read() throws Exception{
con = DBConnection.getDBConnection().getConnection();
pre = con.prepareStatement("select i_img from fw.img where i_id=? and i_img is not null");
pre.setInt(1, 1);
rs = pre.executeQuery();
if(rs.next())
{
img = (BLOB) rs.getBlob("i_img");//获得blob字段
byte[] bt = img.getBytes(1, (int) img.length());//获得blob字段的内容保存到数组bt中
OutputStream os = new FileOutputStream("src/2.jpg");
os.write(bt);
os.flush();
os.close();
System.out.println("读取成功,读出来的图片在src下 2.jpg");
}
DBConnection.getDBConnection().closeConnection(rs, pre, con);
}
public static void main(String[] args) throws Exception {
ImgDAO dao = new ImgDAOImpl();
dao.save();
dao.read();
}
}
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import oracle.sql.BLOB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import util.DBConnection;
import base.dao.BaseDAO;
public class ImgDAOImpl extends BaseDAO implements ImgDAO {
Connection con;
ResultSet rs;
PreparedStatement pre;
BLOB img;//oracle.sql.BLOB类 不能用java.sql.Blob 操纵clob也同理
boolean isSuccess;
public void save() throws Exception {
con = DBConnection.getDBConnection().getConnection();
con.setAutoCommit(false);
pre = con.prepareStatement("insert into fw.img values (?,empty_blob())");
pre.setInt(1, 1);
if(pre.executeUpdate()>0)
{
pre = con.prepareStatement("select i_img from fw.img where i_id=? for update");
pre.setInt(1, 1);
rs = pre.executeQuery();
if(rs.next())
{
img = (BLOB) rs.getBlob("i_img");//获得blob字段
InputStream is = new FileInputStream("src/1.jpg");
byte[] bt = new byte[is.available()];
is.read(bt);
OutputStream os = img.getBinaryOutputStream();
os.write(bt);
os.flush();
is.close();
os.close();
isSuccess=true;
}
if(isSuccess)
{
con.commit();
System.out.println("插入完毕");
}
else
{
con.rollback();
}
}
else
{
con.rollback();
}
DBConnection.getDBConnection().closeConnection(rs, pre, con);
}
public void read() throws Exception{
con = DBConnection.getDBConnection().getConnection();
pre = con.prepareStatement("select i_img from fw.img where i_id=? and i_img is not null");
pre.setInt(1, 1);
rs = pre.executeQuery();
if(rs.next())
{
img = (BLOB) rs.getBlob("i_img");//获得blob字段
byte[] bt = img.getBytes(1, (int) img.length());//获得blob字段的内容保存到数组bt中
OutputStream os = new FileOutputStream("src/2.jpg");
os.write(bt);
os.flush();
os.close();
System.out.println("读取成功,读出来的图片在src下 2.jpg");
}
DBConnection.getDBConnection().closeConnection(rs, pre, con);
}
public static void main(String[] args) throws Exception {
ImgDAO dao = new ImgDAOImpl();
dao.save();
dao.read();
}
}
相关文章推荐
- 以二进制形式将图片保存到数据库,用存诸过程实现(完整代码+sql语句)
- 图片保存到数据库和从数据库读取图片并显示(C#)
- asp中读取数据库中保存的图片存入硬盘
- 图片保存到数据库和从数据库读取图片并显示(C#)
- 图片保存到数据库和从数据库读取图片并显示(C#)
- 如何读取保存在数据库中的图片显示在DataGrid中
- 从数据库中读取一个图片并保存为一个图片文件[转]
- C#保存图片到数据库,读取图片显示
- 从数据库保存和读取图片
- ASP.NET中常用功能代码总结(上传图片到数据库,从数据库中读取图片)
- 将图片保存到ORACEL数据库和读取数据显示到页面
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- 图片保存到数据库和从数据库读取图片并显示(C#)
- 图片保存到数据库和从数据库读取图片并显示
- 如何读取保存在数据库中的图片显示在DataGrid中
- 保存/读取图片到数据库
- 图片保存到数据库和从数据库读取图片并显示(C#)
- web中,如何读取Word内容(包含表格,但不包含图片)并且显示在页面中或保存进数据库中
- word 文档的保存数据及读取,类似图片,数据库设置图片格式(image)
- 从数据库中读取一个图片并保存为一个图片文件