java中的映像存储数据,在Oracle数据库中存储映像的示例

你可以通过PreparedStatement接口将图像存储在Java数据库中。

PreparedStatement的setBinaryStream()方法用于将Binary信息设置为parameterIndex。

setBinaryStream方法的签名

setBinaryStream()方法的语法如下:

1) public void setBinaryStream(int paramIndex, InputStream stream)

throws SQLException

2) public void setBinaryStream(int paramIndex, InputStream stream, long length)

throws SQLException

为了将图像存储到数据库中, 表中使用了BLOB(二进制大对象)数据类型。例如:

CREATE TABLE "IMGTABLE"

("NAME" VARCHAR2(4000), "PHOTO" BLOB

)

/

让我们编写jdbc代码以将图像存储在数据库中。在这里, 我们使用d:\\ d.jpg作为图像的位置。你可以根据图像位置进行更改。

Java示例将图像存储在数据库中

import java.sql.*;

import java.io.*;

public class InsertImage {

public static void main(String[] args) {

try{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con=DriverManager.getConnection(

"jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");

PreparedStatement ps=con.prepareStatement("insert into imgtable values(?, ?)");

ps.setString(1, "sonoo");

FileInputStream fin=new FileInputStream("d:\g.jpg");

ps.setBinaryStream(2, fin, fin.available());

int i=ps.executeUpdate();

System.out.println(i+" records affected");

con.close();

}catch (Exception e) {e.printStackTrace();}

}

}

如果你看到该表, 则记录将存储在数据库中, 但不会显示图像。为此, 你需要从我们将在下一页介绍的数据库中检索图像。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值