Advertisement
gustavozlago

ImportarProdutos

Apr 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.69 KB | None | 0 0
  1. package br.com.gui;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import javax.swing.JOptionPane;
  8.  
  9. /**
  10.  *
  11.  * @author GUSTAVOZLAGO
  12.  */
  13. public class INCLUIRPRODUCAO extends javax.swing.JFrame {
  14.  
  15.     /**
  16.      * Creates new form INCLUIRPRODUCAO
  17.      */
  18.     public INCLUIRPRODUCAO() {
  19.         initComponents();
  20.         setSize(800, 600);
  21.        
  22.     }
  23.  
  24.     /**
  25.      * This method is called from within the constructor to initialize the form.
  26.      * WARNING: Do NOT modify this code. The content of this method is always
  27.      * regenerated by the Form Editor.
  28.      */
  29.     @SuppressWarnings("unchecked")
  30.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  31.     private void initComponents() {
  32.  
  33.         jScrollPane1 = new javax.swing.JScrollPane();
  34.         TblIncluirProducao = new javax.swing.JTable();
  35.         btnSalvar = new javax.swing.JButton();
  36.  
  37.         setResizable(false);
  38.  
  39.         TblIncluirProducao.setModel(new javax.swing.table.DefaultTableModel(
  40.             new Object [][] {
  41.                 {null, null, null, null},
  42.                 {null, null, null, null},
  43.                 {null, null, null, null},
  44.                 {null, null, null, null},
  45.                 {null, null, null, null},
  46.                 {null, null, null, null},
  47.                 {null, null, null, null},
  48.                 {null, null, null, null},
  49.                 {null, null, null, null},
  50.                 {null, null, null, null},
  51.                 {null, null, null, null},
  52.                 {null, null, null, null},
  53.                 {null, null, null, null},
  54.                 {null, null, null, null},
  55.                 {null, null, null, null},
  56.                 {null, null, null, null},
  57.                 {null, null, null, null},
  58.                 {null, null, null, null},
  59.                 {null, null, null, null},
  60.                 {null, null, null, null},
  61.                 {null, null, null, null},
  62.                 {null, null, null, null},
  63.                 {null, null, null, null},
  64.                 {null, null, null, null},
  65.                 {null, null, null, null},
  66.                 {null, null, null, null},
  67.                 {null, null, null, null},
  68.                 {null, null, null, null},
  69.                 {null, null, null, null},
  70.                 {null, null, null, null},
  71.                 {null, null, null, null},
  72.                 {null, null, null, null},
  73.                 {null, null, null, null},
  74.                 {null, null, null, null},
  75.                 {null, null, null, null},
  76.                 {null, null, null, null},
  77.                 {null, null, null, null},
  78.                 {null, null, null, null},
  79.                 {null, null, null, null},
  80.                 {null, null, null, null},
  81.                 {null, null, null, null},
  82.                 {null, null, null, null},
  83.                 {null, null, null, null},
  84.                 {null, null, null, null},
  85.                 {null, null, null, null},
  86.                 {null, null, null, null},
  87.                 {null, null, null, null},
  88.                 {null, null, null, null},
  89.                 {null, null, null, null},
  90.                 {null, null, null, null},
  91.                 {null, null, null, null},
  92.                 {null, null, null, null},
  93.                 {null, null, null, null},
  94.                 {null, null, null, null},
  95.                 {null, null, null, null},
  96.                 {null, null, null, null},
  97.                 {null, null, null, null},
  98.                 {null, null, null, null},
  99.                 {null, null, null, null},
  100.                 {null, null, null, null},
  101.                 {null, null, null, null},
  102.                 {null, null, null, null},
  103.                 {null, null, null, null},
  104.                 {null, null, null, null},
  105.                 {null, null, null, null},
  106.                 {null, null, null, null},
  107.                 {null, null, null, null},
  108.                 {null, null, null, null},
  109.                 {null, null, null, null},
  110.                 {null, null, null, null},
  111.                 {null, null, null, null},
  112.                 {null, null, null, null},
  113.                 {null, null, null, null},
  114.                 {null, null, null, null},
  115.                 {null, null, null, null},
  116.                 {null, null, null, null},
  117.                 {null, null, null, null},
  118.                 {null, null, null, null},
  119.                 {null, null, null, null},
  120.                 {null, null, null, null},
  121.                 {null, null, null, null},
  122.                 {null, null, null, null},
  123.                 {null, null, null, null},
  124.                 {null, null, null, null},
  125.                 {null, null, null, null},
  126.                 {null, null, null, null},
  127.                 {null, null, null, null},
  128.                 {null, null, null, null},
  129.                 {null, null, null, null},
  130.                 {null, null, null, null},
  131.                 {null, null, null, null},
  132.                 {null, null, null, null},
  133.                 {null, null, null, null},
  134.                 {null, null, null, null},
  135.                 {null, null, null, null},
  136.                 {null, null, null, null},
  137.                 {null, null, null, null},
  138.                 {null, null, null, null},
  139.                 {null, null, null, null},
  140.                 {null, null, null, null}
  141.             },
  142.             new String [] {
  143.                 "DOCUMENTO", "FUNCIONÁRIO", "QUANTIDADE", "DATA"
  144.             }
  145.         ) {
  146.             Class[] types = new Class [] {
  147.                 java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
  148.             };
  149.  
  150.             public Class getColumnClass(int columnIndex) {
  151.                 return types [columnIndex];
  152.             }
  153.         });
  154.         TblIncluirProducao.getTableHeader().setReorderingAllowed(false);
  155.         jScrollPane1.setViewportView(TblIncluirProducao);
  156.         if (TblIncluirProducao.getColumnModel().getColumnCount() > 0) {
  157.             TblIncluirProducao.getColumnModel().getColumn(0).setResizable(false);
  158.             TblIncluirProducao.getColumnModel().getColumn(0).setHeaderValue("DOCUMENTO");
  159.             TblIncluirProducao.getColumnModel().getColumn(1).setResizable(false);
  160.             TblIncluirProducao.getColumnModel().getColumn(1).setHeaderValue("FUNCIONÁRIO");
  161.             TblIncluirProducao.getColumnModel().getColumn(2).setResizable(false);
  162.             TblIncluirProducao.getColumnModel().getColumn(2).setHeaderValue("QUANTIDADE");
  163.             TblIncluirProducao.getColumnModel().getColumn(3).setResizable(false);
  164.             TblIncluirProducao.getColumnModel().getColumn(3).setHeaderValue("DATA");
  165.         }
  166.  
  167.         btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/salvar.png"))); // NOI18N
  168.         btnSalvar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
  169.         btnSalvar.setMaximumSize(new java.awt.Dimension(100, 100));
  170.         btnSalvar.setMinimumSize(new java.awt.Dimension(100, 100));
  171.         btnSalvar.setPreferredSize(new java.awt.Dimension(100, 100));
  172.         btnSalvar.addActionListener(new java.awt.event.ActionListener() {
  173.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  174.                 btnSalvarActionPerformed(evt);
  175.             }
  176.         });
  177.  
  178.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  179.         getContentPane().setLayout(layout);
  180.         layout.setHorizontalGroup(
  181.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  182.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  183.                 .addContainerGap()
  184.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
  185.                 .addContainerGap())
  186.             .addGroup(layout.createSequentialGroup()
  187.                 .addGap(340, 340, 340)
  188.                 .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  189.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  190.         );
  191.         layout.setVerticalGroup(
  192.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  193.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  194.                 .addContainerGap()
  195.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
  196.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  197.                 .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  198.                 .addContainerGap())
  199.         );
  200.  
  201.         pack();
  202.         setLocationRelativeTo(null);
  203.     }// </editor-fold>                        
  204.  
  205.     private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  206.  
  207.        int count = 0;
  208.         try {
  209.  
  210.             Class.forName("com.mysql.jdbc.Driver");
  211.  
  212.             Connection con;
  213.  
  214.             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Supram", "root", "123");
  215.  
  216.             String query = "INSERT INTO incluirproducao (Documento, Funcionario, Quantidade, Data) VALUES(?,?,?,?)";
  217.  
  218.             try (PreparedStatement stmt = con.prepareStatement(query)) {
  219.                
  220.                    
  221.                     System.out.println(stmt);
  222.                    
  223.                     stmt.setString(1, TblIncluirProducao.getModel().getValueAt(count, 0).toString());
  224.                     stmt.setString(2, TblIncluirProducao.getModel().getValueAt(count, 1).toString());
  225.                     stmt.setString(3, TblIncluirProducao.getModel().getValueAt(count, 2).toString());
  226.                     stmt.setString(4, TblIncluirProducao.getModel().getValueAt(count, 3).toString());
  227.                    
  228.                    
  229.                    
  230.                     //erro emexecuteUpdate pegar a ultima coluna da tabela do JFrame
  231.                     //nullpointer exception
  232.                     stmt.executeUpdate();
  233.  
  234.                    
  235.                 }
  236.            
  237.             con.close();
  238.             this.dispose();
  239.  
  240.         } catch (ClassNotFoundException ex) {
  241.             System.out.println("Não foi possivel encontrar a classe");
  242.         } catch (SQLException e) {
  243.             JOptionPane.showMessageDialog(this, e.getMessage(), "debug", 1);
  244.             System.out.println("Ocorreu um erro de SQL");
  245.         }        
  246.  
  247.     }                                        
  248.  
  249.     /**
  250.      * @param args the command line arguments
  251.      */
  252.     public static void main(String args[]) {
  253.         /* Set the Nimbus look and feel */
  254.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  255.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  256.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  257.          */
  258.         try {
  259.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  260.                 if ("Nimbus".equals(info.getName())) {
  261.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  262.                     break;
  263.                 }
  264.             }
  265.         } catch (ClassNotFoundException ex) {
  266.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  267.         } catch (InstantiationException ex) {
  268.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  269.         } catch (IllegalAccessException ex) {
  270.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  271.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  272.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  273.         }
  274.         //</editor-fold>
  275.  
  276.         /* Create and display the form */
  277.         java.awt.EventQueue.invokeLater(new Runnable() {
  278.             public void run() {
  279.                 new INCLUIRPRODUCAO().setVisible(true);
  280.             }
  281.         });
  282.     }
  283.  
  284.     // Variables declaration - do not modify                    
  285.     private javax.swing.JTable TblIncluirProducao;
  286.     private javax.swing.JButton btnSalvar;
  287.     private javax.swing.JScrollPane jScrollPane1;
  288.     // End of variables declaration                  
  289.  
  290. }
  291.  
  292. //int count = TblIncluirProducao.getRowCount();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement