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

/*
 * Programme.java
 *
 * Created on 24.04.2010, 17:12:06
 */

package javacity;

import java.sql.Blob;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Simon Köhler
 */
public class Programme extends javax.swing.JFrame implements Runnable {

    /** Creates new form Programme */
    public Programme() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Lueberschrift = new javax.swing.JLabel();
        PBfortschritt = new javax.swing.JProgressBar();

        setTitle("JavaCity - Programme");

        Lueberschrift.setFont(new java.awt.Font("Perpetua", 1, 36));
        Lueberschrift.setText("Programme");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(Lueberschrift))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(PBfortschritt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(210, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Lueberschrift)
                .addGap(95, 95, 95)
                .addComponent(PBfortschritt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(133, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void Lesen(String pfad) throws SQLException, FileNotFoundException {
        DBS.bin(new FileInputStream(new File(pfad)));
    }

    private void Schreiben(String pfad) throws IOException, SQLException {
        //Knöpfe enablen
        ResultSet erg = DBS.abfrage("SELECT Datei FROM bs_Programm WHERE PID = 3");
        System.out.println("1");
//        Blob b = erg.getBlob("Datei");
        System.out.println("2");
        byte[] bbyte = erg.getBlob("Datei").getBytes(0, (int) erg.getBlob("Datei").length());
        System.out.println("3");
        BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(pfad + "JavaCity.jar"));
        System.out.println("4");
        bos.write(bbyte, 0, bbyte.length);
        System.out.println("5");
        bos.flush();
        System.out.println("6");
        bos.close();
    }

    public void run() {
        try {
            Schreiben("/home/schueler/Desktop/");
        } catch (IOException ex) {
            Logger.getLogger(Programme.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Programme.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Programme().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Lueberschrift;
    private javax.swing.JProgressBar PBfortschritt;
    // End of variables declaration//GEN-END:variables

}
