Java tutorial
/* * 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. */ /** * * @author user */ import com.mongodb.*; import com.mongodb.BasicDBObject; import javax.swing.*; public class admin_update_Emp extends javax.swing.JFrame { //admin_change_Emp admin_change = new admin_change_Emp(); /** * Creates new form admin_update_Emp */ /* String mobno_new; String mobno_db; */ String userid_new, userid_db; double temp; public admin_update_Emp(String userid_new2) { initComponents(); //userid_new = admin_change.userid; userid_new = userid_new2; try { MongoClient mc = new MongoClient("localhost", 27017); DB db = mc.getDB("parking_system"); DBCollection collection = db.getCollection("employee_info"); BasicDBObject query = new BasicDBObject(); BasicDBObject field = new BasicDBObject(); field.put("_id", userid_new); DBCursor cursor = collection.find(field); while (cursor.hasNext()) { DBObject obj = (DBObject) cursor.next(); userid_db = obj.get("_id").toString(); System.out.println(userid_db); if (userid_db.equals(userid_new)) { old_name_field.setText(obj.get("name").toString()); old_lname_field.setText(obj.get("lname").toString()); old_mobno_field.setText(obj.get("mobileno").toString()); old_userid_field.setText(obj.get("_id").toString()); old_password_field.setText(obj.get("password").toString()); System.out.println("In if"); break; } } } catch (Exception e) { } } /** * 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() { jLabel1 = new javax.swing.JLabel(); old_name_field = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); old_mobno_field = new javax.swing.JTextField(); old_userid_field = new javax.swing.JTextField(); old_password_field = new javax.swing.JTextField(); jLabel6 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); new_name_field = new javax.swing.JTextField(); new_mobno_field = new javax.swing.JTextField(); jLabel9 = new javax.swing.JLabel(); new_password_field = new javax.swing.JTextField(); jLabel10 = new javax.swing.JLabel(); submit_button = new javax.swing.JButton(); jLabel11 = new javax.swing.JLabel(); new_lname_field = new javax.swing.JTextField(); cancel_button = new javax.swing.JButton(); jLabel12 = new javax.swing.JLabel(); old_lname_field = new javax.swing.JTextField(); jLabel13 = new javax.swing.JLabel(); jLabel14 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel1.setText("Mobile No: "); getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 140, 100, 28)); old_name_field.setEditable(false); old_name_field.setBorder(null); getContentPane().add(old_name_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 60, 140, 28)); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel2.setText("Name: "); getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 70, 27)); jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel3.setText("User ID:"); getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 180, 70, 23)); jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel4.setText("Password:"); getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 220, 90, 28)); jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel5.setText("Old Information"); getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 150, 26)); old_mobno_field.setEditable(false); old_mobno_field.setBorder(null); getContentPane().add(old_mobno_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 140, 140, 27)); old_userid_field.setEditable(false); old_userid_field.setBorder(null); getContentPane().add(old_userid_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 180, 140, 28)); old_password_field.setEditable(false); old_password_field.setBorder(null); getContentPane().add(old_password_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 220, 140, 28)); jLabel6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel6.setText("N_Name:"); getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 70, 80, 30)); jLabel7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel7.setText("N_Mobile No:"); getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 170, -1, 30)); getContentPane().add(new_name_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 70, 151, 31)); getContentPane().add(new_mobno_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 170, 150, 29)); jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel9.setText("N_Password: "); getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 221, 110, 30)); getContentPane().add(new_password_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 220, 152, 28)); jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel10.setText("NEW Information"); getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 9, 170, 30)); submit_button.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N submit_button.setText("Submit"); submit_button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { submit_buttonActionPerformed(evt); } }); getContentPane().add(submit_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 390, 97, 31)); jLabel11.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel11.setText("N_LName :"); getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 120, 90, 30)); getContentPane().add(new_lname_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 120, 150, 30)); cancel_button.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N cancel_button.setText("Cancel"); cancel_button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancel_buttonActionPerformed(evt); } }); getContentPane().add(cancel_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 390, 90, 30)); jLabel12.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel12.setText("Lname :"); getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 70, 20)); old_lname_field.setEditable(false); old_lname_field.setBorder(null); getContentPane().add(old_lname_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 100, 140, 30)); jLabel13.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel13.setText("Note : "); getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 310, -1, -1)); jLabel14.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N jLabel14.setText("User ID is unique. Cannot be Updated...! "); getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 310, 510, 20)); jLabel8.setIcon(new javax.swing.ImageIcon( "C:\\Users\\user\\Desktop\\DBMS Project\\Parking_Project\\Images\\oak-wood.jpg")); // NOI18N jLabel8.setText("Mob No."); getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, -10, 630, 470)); pack(); }// </editor-fold>//GEN-END:initComponents private void submit_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submit_buttonActionPerformed // TODO add your handling code here: try { MongoClient mc = new MongoClient("localhost", 27017); DB db = mc.getDB("parking_system"); DBCollection collection = db.getCollection("employee_info"); BasicDBObject query = new BasicDBObject(); BasicDBObject field = new BasicDBObject(); field.put("_id", userid_new); DBCursor cursor = collection.find(query, field); while (cursor.hasNext()) { DBObject obj = (DBObject) cursor.next(); userid_db = obj.get("_id").toString(); System.out.println(userid_db); if (userid_new.equals(userid_db)) { System.out.println("IN THIS WALA IF"); /* old_name_field.setText(obj.get("name").toString()); old_lname_field.setText(obj.get("lname").toString()); System.out.println("name "+obj.get("name").toString()); System.out.println("name "+obj.get("name").toString()); old_mobno_field.setText(obj.get("mobileno").toString()); old_userid_field.setText(userid_new); old_password_field.setText(obj.get("password").toString()); System.out.println("Done IN IF"); BasicDBObject query = new BasicDBObject(); BasicDBObject field = new BasicDBObject(); field.put("wheeler_2", 1); DBCursor cursor = collection1.find(query, field); */ String newname, newmobno, newuserid, newpassword, newlname; newname = new_name_field.getText(); newlname = new_lname_field.getText(); newmobno = new_mobno_field.getText(); newpassword = new_password_field.getText(); BasicDBObject searchUpdate = new BasicDBObject(); searchUpdate.append("_id", userid_new);/*. append("name",old_name_field.getText()). append("lname", old_lname_field.getText()). append("mobileno", old_mobno_field.getText()). append("password", old_password_field.getText()); */ BasicDBObject UpdateObject = new BasicDBObject(); UpdateObject.append("$set", new BasicDBObject("name", newname)); collection.update(searchUpdate, UpdateObject); UpdateObject.append("$set", new BasicDBObject("lname", newlname)); collection.update(searchUpdate, UpdateObject); UpdateObject.append("$set", new BasicDBObject("mobileno", newmobno)); collection.update(searchUpdate, UpdateObject); UpdateObject.append("$set", new BasicDBObject("password", newpassword)); collection.update(searchUpdate, UpdateObject); JOptionPane.showMessageDialog(null, "Entry Updated..!"); setVisible(false); new Admin_rights().setVisible(true); break; } } System.out.println("Done OUT IF"); } catch (Exception e) { System.err.println(e); } }//GEN-LAST:event_submit_buttonActionPerformed private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_buttonActionPerformed // TODO add your handling code here: setVisible(false); new admin_change_Emp().setVisible(true); }//GEN-LAST:event_cancel_buttonActionPerformed /** * @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(admin_update_Emp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(admin_update_Emp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(admin_update_Emp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(admin_update_Emp.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 admin_update_Emp().setVisible(true); /* Pata Nahi kya hai...Mam se puchna hai... DawNi solutions.... */ } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cancel_button; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JTextField new_lname_field; private javax.swing.JTextField new_mobno_field; private javax.swing.JTextField new_name_field; private javax.swing.JTextField new_password_field; private javax.swing.JTextField old_lname_field; private javax.swing.JTextField old_mobno_field; private javax.swing.JTextField old_name_field; private javax.swing.JTextField old_password_field; private javax.swing.JTextField old_userid_field; private javax.swing.JButton submit_button; // End of variables declaration//GEN-END:variables }