/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datos;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author juareman
 */
/*
 * 

go
Create Table fotos
(
id_foto int primary key,
foto varbinary(max) not null
)
Go
 *  
 */
public class GuardarImagen {

    public void guardarImagen(){
        try {
            // Get connection
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Google", "sa", "adminadmin");

            if (connection != null) {

                PreparedStatement ps = connection.prepareStatement("insert into fotos(id_foto, foto) values (1, ?)");
                try {
                    connection.setAutoCommit(false);
                    File file = new File("ayuda.png");
                    FileInputStream fis = new FileInputStream(file);
                    ps.setBinaryStream(1, fis, (int) file.length());
                    ps.executeUpdate();
                    connection.commit();

                } catch (SQLException es) {
                    ps.close();
                } catch (FileNotFoundException e){
                    System.out.println("aarchivo no encontrado");
                }
            }


        } catch (SQLException se) {
            System.out.println("ERROR: " + se.getMessage());
        }
    }
    
    public void cargarImagen(){
        try {
            // Get connection
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Google", "sa", "adminadmin");

            if (connection != null) {

                PreparedStatement ps = connection.prepareStatement("select foto from fotos where id_foto = 1");
                try {
                    ResultSet resultSet = ps.executeQuery();
                    if (resultSet.next()){
                        File archivo_imagen = new File("copia_ayuda.png");
                        FileOutputStream fos = new FileOutputStream(archivo_imagen);
                        byte[] buffer = new byte[1];
                        InputStream is = resultSet.getBinaryStream("foto"); //la columna "foto" del resultset
                        while (is.read(buffer) > 0) {
                            fos.write(buffer);
                        }
                    }
                        
                    
                } catch (SQLException es) {
                    System.out.println("ha ocurrido un error con sql");
                } catch (FileNotFoundException e){
                    System.out.println("archivo no encontrado");
                } catch (IOException ioe){
                    System.out.println("error al escribir el archivo");
                }
            }


        } catch (SQLException se) {
            System.out.println("ERROR: " + se.getMessage());
        }
    }

    public static void main(String[] args) {
        new GuardarImagen().guardarImagen();
        //new GuardarImagen().cargarImagen();
    }
}

