Read BLOBs data from database : Blob Binary Data JDBC « Database SQL JDBC « Java






Read BLOBs data from database

  
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main {
  static String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
  static String username = "username";
  static String password = "welcome";
  public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    String sql = "SELECT name, description, image FROM pictures ";
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
      String name = resultSet.getString(1);
      String description = resultSet.getString(2);
      File image = new File("D:\\java.gif");
      FileOutputStream fos = new FileOutputStream(image);

      byte[] buffer = new byte[1];
      InputStream is = resultSet.getBinaryStream(3);
      while (is.read(buffer) > 0) {
        fos.write(buffer);
      }
      fos.close();
    }
    conn.close();
  }
}

   
    
  








Related examples in the same category

1.Store BLOBs data into database
2.Insert picture to MySQL
3.Demo Display Binary Data From Database
4.Materialize binary data onto client
5.Blob: JDBC deals with Binary Data
6.Inserting Image in Database Table
7.Blob and JDBC: Image
8.Blob: Image 2
9.Blob: image 3
10.Insert an Image
11.Retrieve an Image
12.Store and retrieve an object from a table
13.Read CLOBs data from database
14.Getting BLOB Data from a Database Table: how to retrieves bytes from a BLOB.
15.Store CLOBs data into database?
16.Getting and Inserting Binary Data into an Database Table