Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.49 KB | None | 0 0
  1. package javacore;
  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.      * This method is called from within the constructor to initialize the form.
  25.      * WARNING: Do NOT modify this code. The content of this method is always
  26.      * regenerated by the Form Editor.
  27.      */
  28.     @SuppressWarnings("unchecked")
  29.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  30.     private void initComponents() {
  31.  
  32.         jScrollPane1 = new javax.swing.JScrollPane();
  33.         TblIncluirProducao = new javax.swing.JTable();
  34.         btnSalvar = new javax.swing.JButton();
  35.  
  36.         setResizable(false);
  37.  
  38.         TblIncluirProducao.setModel(new javax.swing.table.DefaultTableModel(
  39.             new Object [][] {
  40.                 {"String", "String", "String", "String"}
  41.             },
  42.             new String [] {
  43.                 "DOCUMENTO", "FUNCIONÁRIO", "QUANTIDADE", "DATA"
  44.             }
  45.         ) {
  46.             Class[] types = new Class [] {
  47.                 java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
  48.             };
  49.  
  50.             public Class getColumnClass(int columnIndex) {
  51.                 return types [columnIndex];
  52.             }
  53.         });
  54.         TblIncluirProducao.getTableHeader().setReorderingAllowed(false);
  55.         jScrollPane1.setViewportView(TblIncluirProducao);
  56.         if (TblIncluirProducao.getColumnModel().getColumnCount() > 0) {
  57.             TblIncluirProducao.getColumnModel().getColumn(0).setResizable(false);
  58.             TblIncluirProducao.getColumnModel().getColumn(0).setHeaderValue("DOCUMENTO");
  59.             TblIncluirProducao.getColumnModel().getColumn(1).setResizable(false);
  60.             TblIncluirProducao.getColumnModel().getColumn(1).setHeaderValue("FUNCIONÁRIO");
  61.             TblIncluirProducao.getColumnModel().getColumn(2).setResizable(false);
  62.             TblIncluirProducao.getColumnModel().getColumn(2).setHeaderValue("QUANTIDADE");
  63.             TblIncluirProducao.getColumnModel().getColumn(3).setResizable(false);
  64.             TblIncluirProducao.getColumnModel().getColumn(3).setHeaderValue("DATA");
  65.         }
  66.  
  67. //        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/salvar.png"))); // NOI18N
  68.         btnSalvar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
  69.         btnSalvar.setMaximumSize(new java.awt.Dimension(100, 100));
  70.         btnSalvar.setMinimumSize(new java.awt.Dimension(100, 100));
  71.         btnSalvar.setPreferredSize(new java.awt.Dimension(100, 100));
  72.         btnSalvar.addActionListener(new java.awt.event.ActionListener() {
  73.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  74.                 btnSalvarActionPerformed(evt);
  75.             }
  76.         });
  77.  
  78.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  79.         getContentPane().setLayout(layout);
  80.         layout.setHorizontalGroup(
  81.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  83.                 .addContainerGap()
  84.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
  85.                 .addContainerGap())
  86.             .addGroup(layout.createSequentialGroup()
  87.                 .addGap(340, 340, 340)
  88.                 .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  89.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  90.         );
  91.         layout.setVerticalGroup(
  92.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  93.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  94.                 .addContainerGap()
  95.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
  96.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  97.                 .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  98.                 .addContainerGap())
  99.         );
  100.  
  101.         pack();
  102.         setLocationRelativeTo(null);
  103.     }// </editor-fold>                        
  104.  
  105.     private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  106.  
  107.         int count = 0;
  108.         try {
  109.  
  110.             Class.forName("com.mysql.jdbc.Driver");
  111.  
  112.             Connection con;
  113.  
  114.             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
  115.  
  116.             String query = "INSERT INTO incluirproducao (Documento, Funcionario, Quantidade, Data) VALUES(?,?,?,?)";
  117.  
  118.             try (PreparedStatement stmt = con.prepareStatement(query)) {
  119.                
  120.                    
  121.                     System.out.println(stmt);
  122.                    
  123.                     stmt.setString(1, TblIncluirProducao.getModel().getValueAt(count, 0).toString());
  124.                     stmt.setString(2, TblIncluirProducao.getModel().getValueAt(count, 0).toString());
  125.                     stmt.setString(3, TblIncluirProducao.getModel().getValueAt(count, 0).toString());
  126.                     stmt.setString(4, TblIncluirProducao.getModel().getValueAt(count, 0).toString());
  127.                    
  128.                    
  129.                    
  130.                     //erro emexecuteUpdate pegar a ultima coluna da tabela do JFrame
  131.                     //nullpointer exception
  132.                     stmt.executeUpdate();
  133.  
  134.                    
  135.                 }
  136.            
  137.             con.close();
  138.             this.dispose();
  139.  
  140.         } catch (ClassNotFoundException ex) {
  141.             System.out.println("Não foi possivel encontrar a classe");
  142.         } catch (SQLException e) {
  143.             JOptionPane.showMessageDialog(this, e.getMessage(), "debug", 1);
  144.             System.out.println("Ocorreu um erro de SQL");
  145.         }
  146.  
  147.  
  148.     }                                        
  149.  
  150.     /**
  151.      * @param args the command line arguments
  152.      */
  153.     public static void main(String args[]) {
  154.         /* Set the Nimbus look and feel */
  155.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  156.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  157.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  158.          */
  159.         try {
  160.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  161.                 if ("Nimbus".equals(info.getName())) {
  162.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  163.                     break;
  164.                 }
  165.             }
  166.         } catch (ClassNotFoundException ex) {
  167.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  168.         } catch (InstantiationException ex) {
  169.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  170.         } catch (IllegalAccessException ex) {
  171.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  172.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  173.             java.util.logging.Logger.getLogger(INCLUIRPRODUCAO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  174.         }
  175.         //</editor-fold>
  176.  
  177.         /* Create and display the form */
  178.         java.awt.EventQueue.invokeLater(new Runnable() {
  179.             public void run() {
  180.                 new INCLUIRPRODUCAO().setVisible(true);
  181.             }
  182.         });
  183.     }
  184.  
  185.     // Variables declaration - do not modify                    
  186.     private javax.swing.JTable TblIncluirProducao;
  187.     private javax.swing.JButton btnSalvar;
  188.     private javax.swing.JScrollPane jScrollPane1;
  189.     // End of variables declaration                  
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement