[英]How to retrieve image from Oracle Database using JDBC?
I'm using JDBC to connect to the oracle database.我正在使用 JDBC 连接到 oracle 数据库。 I know how to retrieve various data (strings, ints, etc.), but I don't know how to get ORDImage from this database.
我知道如何检索各种数据(字符串、整数等),但我不知道如何从此数据库中获取 ORDImage。 Is there any ResultSet method to accomplish this?
是否有任何 ResultSet 方法来完成此操作?
getBlob() of PreparedStatement is used to get Binary information, it returns the instance of Blob. PreparedStatement 的 getBlob() 用于获取 Binary 信息,它返回 Blob 的实例。 By calling the getBytes() method on the blob object, you will get the array of binary information that can be written into the image file.
通过对 blob 对象调用 getBytes() 方法,您将获得可写入图像文件的二进制信息数组。
Here is the code..这是代码..
PreparedStatement ps = con.prepareStatement("select * from MyImageTable");
ResultSet rs = ps.executeQuery();
if(rs.next()){
Blob blob = rs.getBlob(2); //Here 2 is second column data
byte b[] = blob.getBytes(1,(int)b.length()); //Here 1 represent first image
FileOutputStream output = new FileOutputStream("e:\\some_pic.jpg"); //path goes here
output.write(b);
output.close();
Check out the streaming lobs section of the oracle jdbc developers guide.查看 oracle jdbc 开发人员指南的流 lob部分。 There are several ways to access the data.
有几种方法可以访问数据。 There are different memory/performance impacts of the different options.
不同的选项有不同的内存/性能影响。 The simplest solution is probably ResultSet.getBinaryStream .
最简单的解决方案可能是ResultSet.getBinaryStream 。 But if you want information about length of data, you can also use getBlob .
但是,如果您需要有关数据长度的信息,也可以使用getBlob 。
There are lot of ways to display to retrieve image from database in java library, this one is of the best way to retrieve image from database to java swing form and can display in JLable according to your required size. java库中从数据库中检索图像的显示方式有很多种,这一种是从数据库中检索图像到java swing格式的最佳方式,可以根据您需要的大小在JLable中显示。
ResultSet rs = stmnt.executeQuery("select * from table1 ");
Blob blob= rs.getBlob("image");
byte bt[]= blob.getBytes(1,(int)blob.length());
Image img= ImageIO.read(new ByteArrayInputStream(bt));
Image img1= img.getScaledInstance(200,230,Image.SCALE_SMOOTH);
ImageIcon icon= new ImageIcon(img1);
JLable1.setIcon(icon);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.