projetocsv.framePrincipal.java Source code

Java tutorial

Introduction

Here is the source code for projetocsv.framePrincipal.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package projetocsv;

import com.opencsv.CSVWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.commons.net.ftp.FTPClient;
import projetocsv.controller.Oracle;
//import projetocsv.controller.ExportCSV;

/**
 *
 * @author Winston
 */
public class framePrincipal extends javax.swing.JFrame {

    /**
     * Creates new form framePrincipal
     */
    public framePrincipal() {
        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() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Gerar e enviar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(146, 146, 146).addComponent(jButton1)
                        .addContainerGap(153, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(138, 138, 138).addComponent(jButton1)
                        .addContainerGap(139, Short.MAX_VALUE)));

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Oracle conexao = new Oracle("localhost", "winston", "winston");
        if (conexao.connect()) {
            JOptionPane.showMessageDialog(null, "Conectado");

            //ExportCSV csv = new ExportCSV();
            //csv.gerar("teste.csv", conexao);
            CSVWriter writer = null;
            try {
                writer = new CSVWriter(new FileWriter("teste.csv"), '\t', ';');
            } catch (IOException ex) {
                Logger.getLogger(framePrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }
            Boolean includeHeaders = false;

            java.sql.ResultSet myResultSet = conexao.executar("select * from testtable");

            try {
                writer.writeAll(myResultSet, includeHeaders);

            } catch (SQLException ex) {
                Logger.getLogger(framePrincipal.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(framePrincipal.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                JOptionPane.showMessageDialog(null, "Gerao finalizada");
                conexao.disconnect();
            }

            try {
                writer.close();
                FTPClient ftp = new FTPClient();
                try {
                    ftp.connect("ftp.testeftpm.xp3.biz");
                    ftp.login("testeftpm.xp3.biz ", "passftp");
                    System.out.println("Ftp conectado");
                } catch (Exception e) {
                    System.out.println("Erro ao conectar");
                }

                FileInputStream arqEnviar = new FileInputStream("teste.csv");
                if (ftp.storeFile("teste.csv", arqEnviar)) {
                    System.out.println("Arquivo enviado com sucesso!");
                    JOptionPane.showMessageDialog(null, "Arquivo enviado com sucesso!");
                } else {
                    System.out.println("Erro ao enviar arquivo.");
                }

            } catch (IOException ex) {
                Logger.getLogger(framePrincipal.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            JOptionPane.showMessageDialog(null, "Erro ao estabelecer conexo");
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(framePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(framePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(framePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(framePrincipal.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new framePrincipal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
}