NightRaven97

db update delete

Apr 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.01 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package sample25;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.JOptionPane;
  14.  
  15. /**
  16.  *
  17.  * @author ONEYES
  18.  */
  19. public class NewApplication extends javax.swing.JFrame {
  20.  
  21.     /**
  22.      * Creates new form NewApplication
  23.      */
  24.     public NewApplication() {
  25.         initComponents();
  26.     }
  27.  
  28.     /**
  29.      * This method is called from within the constructor to initialize the form.
  30.      * WARNING: Do NOT modify this code. The content of this method is always
  31.      * regenerated by the Form Editor.
  32.      */
  33.     @SuppressWarnings("unchecked")
  34.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  35.     private void initComponents() {
  36.  
  37.         jLabel1 = new javax.swing.JLabel();
  38.         jLabel2 = new javax.swing.JLabel();
  39.         txtname = new javax.swing.JTextField();
  40.         txtclass = new javax.swing.JTextField();
  41.         bsave = new javax.swing.JButton();
  42.         bcancel = new javax.swing.JButton();
  43.         update = new javax.swing.JButton();
  44.         delete1 = new javax.swing.JButton();
  45.         menuBar = new javax.swing.JMenuBar();
  46.         fileMenu = new javax.swing.JMenu();
  47.         openMenuItem = new javax.swing.JMenuItem();
  48.         saveMenuItem = new javax.swing.JMenuItem();
  49.         saveAsMenuItem = new javax.swing.JMenuItem();
  50.         exitMenuItem = new javax.swing.JMenuItem();
  51.         editMenu = new javax.swing.JMenu();
  52.         cutMenuItem = new javax.swing.JMenuItem();
  53.         copyMenuItem = new javax.swing.JMenuItem();
  54.         pasteMenuItem = new javax.swing.JMenuItem();
  55.         deleteMenuItem = new javax.swing.JMenuItem();
  56.         helpMenu = new javax.swing.JMenu();
  57.         contentsMenuItem = new javax.swing.JMenuItem();
  58.         aboutMenuItem = new javax.swing.JMenuItem();
  59.  
  60.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  61.  
  62.         jLabel1.setText("Name");
  63.  
  64.         jLabel2.setText("Class");
  65.  
  66.         txtname.setText("  ");
  67.  
  68.         bsave.setText("Save");
  69.         bsave.addActionListener(new java.awt.event.ActionListener() {
  70.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  71.                 bsaveActionPerformed(evt);
  72.             }
  73.         });
  74.  
  75.         bcancel.setText("Cancel");
  76.         bcancel.addActionListener(new java.awt.event.ActionListener() {
  77.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  78.                 bcancelActionPerformed(evt);
  79.             }
  80.         });
  81.  
  82.         update.setText("update");
  83.         update.addActionListener(new java.awt.event.ActionListener() {
  84.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  85.                 updateActionPerformed(evt);
  86.             }
  87.         });
  88.  
  89.         delete1.setText("delete");
  90.         delete1.addActionListener(new java.awt.event.ActionListener() {
  91.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  92.                 delete1ActionPerformed(evt);
  93.             }
  94.         });
  95.  
  96.         fileMenu.setMnemonic('f');
  97.         fileMenu.setText("File");
  98.  
  99.         openMenuItem.setMnemonic('o');
  100.         openMenuItem.setText("Open");
  101.         fileMenu.add(openMenuItem);
  102.  
  103.         saveMenuItem.setMnemonic('s');
  104.         saveMenuItem.setText("Save");
  105.         fileMenu.add(saveMenuItem);
  106.  
  107.         saveAsMenuItem.setMnemonic('a');
  108.         saveAsMenuItem.setText("Save As ...");
  109.         saveAsMenuItem.setDisplayedMnemonicIndex(5);
  110.         fileMenu.add(saveAsMenuItem);
  111.  
  112.         exitMenuItem.setMnemonic('x');
  113.         exitMenuItem.setText("Exit");
  114.         exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
  115.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  116.                 exitMenuItemActionPerformed(evt);
  117.             }
  118.         });
  119.         fileMenu.add(exitMenuItem);
  120.  
  121.         menuBar.add(fileMenu);
  122.  
  123.         editMenu.setMnemonic('e');
  124.         editMenu.setText("Edit");
  125.  
  126.         cutMenuItem.setMnemonic('t');
  127.         cutMenuItem.setText("Cut");
  128.         editMenu.add(cutMenuItem);
  129.  
  130.         copyMenuItem.setMnemonic('y');
  131.         copyMenuItem.setText("Copy");
  132.         editMenu.add(copyMenuItem);
  133.  
  134.         pasteMenuItem.setMnemonic('p');
  135.         pasteMenuItem.setText("Paste");
  136.         editMenu.add(pasteMenuItem);
  137.  
  138.         deleteMenuItem.setMnemonic('d');
  139.         deleteMenuItem.setText("Delete");
  140.         editMenu.add(deleteMenuItem);
  141.  
  142.         menuBar.add(editMenu);
  143.  
  144.         helpMenu.setMnemonic('h');
  145.         helpMenu.setText("Help");
  146.  
  147.         contentsMenuItem.setMnemonic('c');
  148.         contentsMenuItem.setText("Contents");
  149.         helpMenu.add(contentsMenuItem);
  150.  
  151.         aboutMenuItem.setMnemonic('a');
  152.         aboutMenuItem.setText("About");
  153.         helpMenu.add(aboutMenuItem);
  154.  
  155.         menuBar.add(helpMenu);
  156.  
  157.         setJMenuBar(menuBar);
  158.  
  159.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  160.         getContentPane().setLayout(layout);
  161.         layout.setHorizontalGroup(
  162.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  163.             .addGroup(layout.createSequentialGroup()
  164.                 .addGap(74, 74, 74)
  165.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  166.                     .addGroup(layout.createSequentialGroup()
  167.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  168.                             .addComponent(jLabel2)
  169.                             .addComponent(jLabel1))
  170.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  171.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  172.                             .addComponent(txtname, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
  173.                             .addComponent(txtclass))
  174.                         .addGap(109, 109, 109))
  175.                     .addGroup(layout.createSequentialGroup()
  176.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  177.                             .addComponent(delete1)
  178.                             .addGroup(layout.createSequentialGroup()
  179.                                 .addComponent(bsave)
  180.                                 .addGap(62, 62, 62)
  181.                                 .addComponent(bcancel)))
  182.                         .addGap(30, 30, 30)
  183.                         .addComponent(update)
  184.                         .addContainerGap(45, Short.MAX_VALUE))))
  185.         );
  186.         layout.setVerticalGroup(
  187.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  188.             .addGroup(layout.createSequentialGroup()
  189.                 .addGap(70, 70, 70)
  190.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  191.                     .addComponent(jLabel1)
  192.                     .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  193.                 .addGap(47, 47, 47)
  194.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  195.                     .addComponent(jLabel2)
  196.                     .addComponent(txtclass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  197.                 .addGap(50, 50, 50)
  198.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  199.                     .addComponent(bsave)
  200.                     .addComponent(bcancel)
  201.                     .addComponent(update))
  202.                 .addGap(18, 18, 18)
  203.                 .addComponent(delete1)
  204.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  205.         );
  206.  
  207.         pack();
  208.     }// </editor-fold>
  209.  
  210.     private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                            
  211.         System.exit(0);
  212.     }                                            
  213.  
  214.     private void bsaveActionPerformed(java.awt.event.ActionEvent evt) {                                      
  215.         try {
  216.             // TODO add your handling code here:
  217.            
  218.          String s=txtname.getText();
  219.                 String r= txtclass.getText();
  220.              
  221.  
  222.                   Class.forName("com.mysql.jdbc.Driver");
  223.                   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift","root","admin");
  224.                   String query ="insert into gift1 VALUES(?,?)";
  225.                   PreparedStatement pst=con.prepareStatement(query);
  226.                 pst.setString(1,s);
  227.                 pst.setString(2,r);
  228.                int count=pst.executeUpdate();
  229.                if(count>0)
  230.                {
  231.                    dialog("saved successfully");
  232.                }
  233.                 con.close();
  234.            
  235.             clear();
  236.         } catch (SQLException ex) {
  237.             Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
  238.         } catch (ClassNotFoundException ex) {
  239.             Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
  240.         }
  241.     }                                    
  242.  
  243.    
  244.      void dialog(String m)
  245.     {
  246.         JOptionPane.showMessageDialog(rootPane, m);
  247.     }
  248.    
  249.    
  250.     void clear(){
  251.      
  252.     txtname.setText(" ");
  253.     txtclass.setText(" ");
  254.    
  255. }
  256.     private void bcancelActionPerformed(java.awt.event.ActionEvent evt) {                                        
  257.         // TODO add your handling code here:
  258.        
  259.         clear();
  260.        
  261.     }                                      
  262.  
  263.     private void updateActionPerformed(java.awt.event.ActionEvent evt) {
  264.         try {
  265.             String id1= txtname.getText();
  266.             String nam=txtclass.getText();
  267.              Class.forName("com.mysql.jdbc.Driver");
  268.                       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift","root","admin");
  269.                       String query ="update gift1 set nam=? where id1=?";
  270.                       PreparedStatement pst=con.prepareStatement(query);
  271.                     pst.setString(1,nam);
  272.                     pst.setString(2,id1);
  273.                    int count=pst.executeUpdate();
  274.                    if(count>0)
  275.                    {
  276.                        dialog("saved successfully");
  277.                    }
  278.                     con.close();
  279.                
  280.            
  281.             // TODO add your handling code here:
  282.         } catch (SQLException ex) {
  283.             Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
  284.         } catch (ClassNotFoundException ex) {
  285.             Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
  286.         }
  287.     }
  288.  
  289.     private void delete1ActionPerformed(java.awt.event.ActionEvent evt) {
  290.         String id1= txtname.getText();
  291.         try {
  292.                        
  293.             Class.forName("com.mysql.jdbc.Driver");
  294.                           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gift","root","admin");
  295.                           String query ="delete from gift1 where id1=?";
  296.                           PreparedStatement pst=con.prepareStatement(query);
  297.                           pst.setString(1,id1);
  298.                        int count=pst.executeUpdate();
  299.                        if(count>0)
  300.                        {
  301.                            dialog("saved successfully");
  302.                        }
  303.                         con.close();
  304.            
  305.             // TODO add your handling code here:
  306.         } catch (SQLException ex) {
  307.             Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
  308.         } catch (ClassNotFoundException ex) {
  309.             Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex);
  310.         }
  311.     }
  312.  
  313.     /**
  314.      * @param args the command line arguments
  315.      */
  316.     public static void main(String args[]) {
  317.         /*
  318.          * Set the Nimbus look and feel
  319.          */
  320.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  321.         /*
  322.          * If Nimbus (introduced in Java SE 6) is not available, stay with the
  323.          * default look and feel. For details see
  324.          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  325.          */
  326.         try {
  327.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  328.                 if ("Nimbus".equals(info.getName())) {
  329.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  330.                     break;
  331.                 }
  332.             }
  333.         } catch (ClassNotFoundException ex) {
  334.             java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  335.         } catch (InstantiationException ex) {
  336.             java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  337.         } catch (IllegalAccessException ex) {
  338.             java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  339.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  340.             java.util.logging.Logger.getLogger(NewApplication.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  341.         }
  342.         //</editor-fold>
  343.  
  344.         /*
  345.          * Create and display the form
  346.          */
  347.         java.awt.EventQueue.invokeLater(new Runnable() {
  348.  
  349.             public void run() {
  350.                 new NewApplication().setVisible(true);
  351.             }
  352.         });
  353.     }
  354.     // Variables declaration - do not modify
  355.     private javax.swing.JMenuItem aboutMenuItem;
  356.     private javax.swing.JButton bcancel;
  357.     private javax.swing.JButton bsave;
  358.     private javax.swing.JMenuItem contentsMenuItem;
  359.     private javax.swing.JMenuItem copyMenuItem;
  360.     private javax.swing.JMenuItem cutMenuItem;
  361.     private javax.swing.JButton delete1;
  362.     private javax.swing.JMenuItem deleteMenuItem;
  363.     private javax.swing.JMenu editMenu;
  364.     private javax.swing.JMenuItem exitMenuItem;
  365.     private javax.swing.JMenu fileMenu;
  366.     private javax.swing.JMenu helpMenu;
  367.     private javax.swing.JLabel jLabel1;
  368.     private javax.swing.JLabel jLabel2;
  369.     private javax.swing.JMenuBar menuBar;
  370.     private javax.swing.JMenuItem openMenuItem;
  371.     private javax.swing.JMenuItem pasteMenuItem;
  372.     private javax.swing.JMenuItem saveAsMenuItem;
  373.     private javax.swing.JMenuItem saveMenuItem;
  374.     private javax.swing.JTextField txtclass;
  375.     private javax.swing.JTextField txtname;
  376.     private javax.swing.JButton update;
  377.     // End of variables declaration
  378. }
Add Comment
Please, Sign In to add comment