Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: How to read BLOBs data from database

Java: How to read BLOBs data from database
{filelink=7033}

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;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
public class BOLOB_RD 
{
  public static void main(String[] argv) throws Exception
  	 {
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cours",
        "root", "");
    String sql = "SELECT name,  image FROM Test where image<>''";
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet res = stmt.executeQuery();
    while (res.next())
    {
      String name= res.getString(1);
      // save image to the file
      File image = new File("D:/java.gif");
      FileOutputStream out_strm = new FileOutputStream(image);
      byte[] buffer = new byte[1];
      InputStream in_strm = res.getBinaryStream(2);
      while (in_strm.read(buffer) > 0)
      {
        out_strm.write(buffer);
      }
      out_strm.close();
    }
    conn.close();
  }
}

No comments yet.

Leave a Comment