简体   繁体   English

如何使用 JDBC 从 Oracle 数据库中检索图像?

[英]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);

Example例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java-如何在不使用JDBC的情况下从数据库检索结果集? - java- how to retrieve resultset from database without using JDBC? 如何使用 JDBC 从数据库中检索 SDO_GEOMETRY? - How to retrieve SDO_GEOMETRY from database using JDBC? 如何使用JDBC从两个不同的MySQL数据库中检索数据? - How to Retrieve Data from Two Different MySQL Database using JDBC? 使用Spring MVC和Hibernate无法从Oracle数据库检索图像 - Unable to retrieve image from oracle database using spring mvc and hibernate 在Spring Security中,如何使用jdbc从Oracle数据库认证用户? - How to authenticate users from an oracle database using jdbc in spring security? 如何从Oracle数据库检索图像并在Struts中的JSP中显示 - How to retrieve image from an Oracle database and display in JSP in Struts 如何将数据库中的元素检索到 JDBC 程序中 - How to retrieve elements from a database into a JDBC program 使用JDBC从Java使用Oracle数据库解析器 - Using the Oracle database parser from Java using JDBC 可以使用JDBC从数据库中检索一条记录,但不能检索另一条记录 - Can retrieve one record from database but not another using JDBC 如何从数据库中检索数据并使用jdbc连接将其显示在jsp文本字段中 - how to Retrieve data from database and display it in a jsp text fields using jdbc connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM