Package AlMaGe

Source Code of AlMaGe.FotoTest

package AlMaGe;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class FotoTest extends JFrame{
 
//  public static void main(String[] args) throws Exception {
//    new FotoTest();
//  }
 
  public FotoTest() throws Exception {
    uploadFoto();
    downloadFoto();
    JPanel p1 = new JPanel();
    p1.paint(getGraphics());
    add(p1, BorderLayout.CENTER);
    pack();
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
 
  private void uploadFoto() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, FileNotFoundException{
    Connection conn = DataMgr.getInstance().getConnection();
    // create a file object for image by specifying full path of image as parameter.
    File image = new File("C:/Users/Matze/Desktop/Picture0002.jpg");
    /* prepareStatement() is used for create statement object that is
    used for sending sql statements to the specified database. */
    PreparedStatement stmt = conn.prepareStatement
    ("UPDATE Personen SET Foto = "+ "? WHERE PersID = 1");
    FileInputStream fis = new FileInputStream(image);
    stmt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));
    /* executeUpdate() method execute specified sql query. Here this query
    insert data and image from specified address. */
    int s = stmt.executeUpdate();
    if(s>0) {
    System.out.println("Uploaded successfully !");
    }
    else {
    System.out.println("unsucessfull to upload image.");
    }
  }
 
    public BufferedImage downloadFoto() throws Exception {
      Statement stmt = DataMgr.getInstance().getStatement();
      ResultSet rs = stmt.executeQuery("SELECT Foto FROM Personen WHERE PersID='1'");
        if (!rs.next()) {
          System.out.println("Image not found");
        }
       
        InputStream in = rs.getBinaryStream(1);
        BufferedImage img = ImageIO.read(in);
       
        System.out.println("Image retrieved ok");
        return img;
    }
     

    public void paint(Graphics g) {
            BufferedImage picture1;
      try {
        picture1 = downloadFoto();
            g.drawImage(picture1,0,0,this);
      } catch (Exception e) {

        e.printStackTrace();
        JOptionPane.showMessageDialog(
            null,

            e.getMessage()
                + System.getProperty("line.separator")
                + "Dies ist ein schwerwiegender Fehler."
                + System.getProperty("line.separator")
                + "Das Programm wird beendet.",
            "Fehler", JOptionPane.WARNING_MESSAGE);
        System.exit(1);

      }
    }
}

TOP

Related Classes of AlMaGe.FotoTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.