jdbc保存图片到数据库并读取代码

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();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Java 读取图片并将其保存到 MySQL 数据库的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class ImageToMysql { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/db_name"; String user = "username"; String password = "password"; Connection conn = null; PreparedStatement ps = null; FileInputStream fis = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); File image = new File("path/to/image.jpg"); fis = new FileInputStream(image); ps = conn.prepareStatement("INSERT INTO images (id, name, data) VALUES (?, ?, ?)"); ps.setInt(1, 1); ps.setString(2, "image.jpg"); ps.setBinaryStream(3, fis, (int) image.length()); ps.executeUpdate(); System.out.println("Image saved to database."); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) fis.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } } ``` 在这个示例中,我们将一个名为 `image.jpg` 的图片文件保存到名为 `images` 的 MySQL 数据库中。我们使用 `FileInputStream` 读取图片文件,然后将其作为二进制数据插入到数据库中。 请注意,这只是一个简单的示例,实际应用中可能需要更复杂的处理和错误处理。同时,也需要在数据库中创建适当的表结构来存储图像数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值