Advertisement
Guest User

ConsultarpDF

a guest
Mar 6th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.84 KB | None | 0 0
  1. package pdf;
  2.  
  3. import java.awt.Desktop;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.sql.Blob;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import static java.util.logging.Logger.global;
  15. import javax.swing.JOptionPane;
  16. import org.omg.CORBA.portable.InputStream;
  17. import softwaremecanica.conectar;
  18.  
  19. /**
  20.  *
  21.  * @author SASAGA
  22.  */
  23.  
  24.  
  25.  
  26.  
  27. public class RecuperarPdf extends javax.swing.JFrame {
  28.  
  29.     /**
  30.      * Creates new form RecuperarPdf
  31.      */
  32.    
  33.    
  34.    
  35.    
  36.     public void BorrarFicheros(){
  37.          String SourcePath="C:\\Program Files\\EMILIANO\\pdf";
  38.          File path = new File(SourcePath);
  39.           File[] ficheros = path.listFiles();
  40.          File f=null;
  41.          
  42.          if(path.exists()){
  43.              for(int x=0;x<ficheros.length;x++){
  44.                  f=new File(ficheros[x].toString());
  45.                  f.delete();
  46.              }
  47.          }else{
  48.            
  49.          }
  50.          }
  51.      
  52.         public void abrirArchivo(){
  53.  
  54. try{
  55.  File folder = new File("C:\\Program Files\\EMILIANO\\pdf");
  56.  String pathname = folder+"\\"+Codigo.getText()+".pdf";
  57.  
  58. File path = new File(pathname);
  59. Desktop.getDesktop().open(path);
  60. }catch(IOException e){
  61. e.printStackTrace();
  62. System.out.println("Ruta erronea");
  63. }
  64. }
  65.        
  66.     public void bajar_archivo()throws SQLException, IOException{
  67.         conectar cc=new conectar();
  68.         Connection cn=cc.conexion();
  69.         Statement st=cn.createStatement();          
  70.         String sql = "SELECT HojaVida FROM documentacion WHERE CodigoUsuario="+Codigo.getText()+"";
  71.          ResultSet rs=st.executeQuery(sql);
  72.          
  73.          try {
  74.              rs.next();
  75.              File folder = new File("C:\\Program Files\\EMILIANO\\pdf");
  76.              folder.mkdir();
  77.              
  78.             String pathname = folder+"\\"+Codigo.getText()+".pdf";
  79.             File file = new File(pathname);
  80.              FileOutputStream output = new FileOutputStream(file);
  81.              Blob archivo = rs.getBlob(1);
  82.              java.io.InputStream inStream = archivo.getBinaryStream();
  83.              int lenght = -1;
  84.              int size =(int)archivo.length();
  85.              byte[]buffer = new byte[size];
  86.              while((lenght = inStream.read(buffer))!=-1){
  87.                  output.write(buffer,0,lenght);
  88.              
  89.              output.close();
  90.            
  91.                
  92.              }
  93.         } catch (Exception ioe) {
  94.             throw  new IOException(ioe.getMessage());
  95.         }
  96.        
  97.     }
  98.    
  99.    
  100.     public RecuperarPdf() {
  101.         initComponents();
  102.     }
  103.  
  104.     /**
  105.      * This method is called from within the constructor to initialize the form.
  106.      * WARNING: Do NOT modify this code. The content of this method is always
  107.      * regenerated by the Form Editor.
  108.      */
  109.     @SuppressWarnings("unchecked")
  110.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  111.     private void initComponents() {
  112.  
  113.         jButton1 = new javax.swing.JButton();
  114.         Codigo = new javax.swing.JTextField();
  115.         jButton2 = new javax.swing.JButton();
  116.  
  117.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  118.  
  119.         jButton1.setText("Visualizar");
  120.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  121.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  122.                 jButton1ActionPerformed(evt);
  123.             }
  124.         });
  125.  
  126.         Codigo.addActionListener(new java.awt.event.ActionListener() {
  127.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  128.                 CodigoActionPerformed(evt);
  129.             }
  130.         });
  131.  
  132.         jButton2.setText("Salir");
  133.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  134.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  135.                 jButton2ActionPerformed(evt);
  136.             }
  137.         });
  138.  
  139.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  140.         getContentPane().setLayout(layout);
  141.         layout.setHorizontalGroup(
  142.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  143.             .addGroup(layout.createSequentialGroup()
  144.                 .addGap(109, 109, 109)
  145.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  146.                     .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
  147.                     .addComponent(Codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
  148.                 .addGap(46, 46, 46)
  149.                 .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
  150.                 .addContainerGap(35, Short.MAX_VALUE))
  151.         );
  152.         layout.setVerticalGroup(
  153.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  154.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  155.                 .addGap(37, 37, 37)
  156.                 .addComponent(Codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  157.                 .addGap(79, 79, 79)
  158.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  159.                     .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
  160.                     .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
  161.                 .addContainerGap(107, Short.MAX_VALUE))
  162.         );
  163.  
  164.         pack();
  165.     }// </editor-fold>                        
  166.  
  167.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  168.         try {
  169.             // TODO add your handling code here:
  170.             bajar_archivo();
  171.         } catch (SQLException ex) {
  172.             Logger.getLogger(RecuperarPdf.class.getName()).log(Level.SEVERE, null, ex);
  173.         } catch (IOException ex) {
  174.             Logger.getLogger(RecuperarPdf.class.getName()).log(Level.SEVERE, null, ex);
  175.         }
  176.        
  177.         abrirArchivo();
  178.      
  179.     }                                        
  180.  
  181.     private void CodigoActionPerformed(java.awt.event.ActionEvent evt) {                                      
  182.         // TODO add your handling code here:
  183.     }                                      
  184.  
  185.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  186.         BorrarFicheros();
  187.         dispose();
  188.     }                                        
  189.  
  190.     /**
  191.      * @param args the command line arguments
  192.      */
  193.     public static void main(String args[]) {
  194.         /* Set the Nimbus look and feel */
  195.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  196.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  197.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  198.          */
  199.         try {
  200.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  201.                 if ("Nimbus".equals(info.getName())) {
  202.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  203.                     break;
  204.                 }
  205.             }
  206.         } catch (ClassNotFoundException ex) {
  207.             java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  208.         } catch (InstantiationException ex) {
  209.             java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  210.         } catch (IllegalAccessException ex) {
  211.             java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  212.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  213.             java.util.logging.Logger.getLogger(RecuperarPdf.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  214.         }
  215.         //</editor-fold>
  216.  
  217.         /* Create and display the form */
  218.         java.awt.EventQueue.invokeLater(new Runnable() {
  219.             public void run() {
  220.                 new RecuperarPdf().setVisible(true);
  221.             }
  222.         });
  223.     }
  224.  
  225.     // Variables declaration - do not modify                    
  226.     private javax.swing.JTextField Codigo;
  227.     private javax.swing.JButton jButton1;
  228.     private javax.swing.JButton jButton2;
  229.     // End of variables declaration                  
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement