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. */ package usermanager; import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.WriteResult; /** * * @author PDK */ public class Main extends javax.swing.JFrame { /** * Creates new form Main */ public Main() { initComponents(); } /** * 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() { lblId = new javax.swing.JLabel(); lblFirstName = new javax.swing.JLabel(); lblLastName = new javax.swing.JLabel(); lblEmail = new javax.swing.JLabel(); txtId = new javax.swing.JTextField(); txtFirstName = new javax.swing.JTextField(); txtLastName = new javax.swing.JTextField(); txtEmail = new javax.swing.JTextField(); btnAddUser = new javax.swing.JButton(); btnUpdateUser = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); lblId.setText("ID"); lblFirstName.setText("First Name"); lblLastName.setText("Last Name"); lblEmail.setText("Email"); btnAddUser.setText("Add User"); btnAddUser.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddUserActionPerformed(evt); } }); btnUpdateUser.setText("Update User"); btnUpdateUser.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnUpdateUserActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(47, 47, 47).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(lblId) .addComponent(lblEmail).addComponent(lblLastName).addComponent(lblFirstName)) .addGap(69, 69, 69).addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtFirstName).addComponent(txtLastName) .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE) .addComponent(txtId))) .addGroup(layout.createSequentialGroup().addGap(107, 107, 107).addComponent(btnAddUser) .addGap(28, 28, 28).addComponent(btnUpdateUser))) .addContainerGap(95, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(72, 72, 72) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblId).addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblFirstName).addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(lblLastName).addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblEmail) .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(32, 32, 32) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnAddUser).addComponent(btnUpdateUser)) .addContainerGap(39, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnAddUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddUserActionPerformed // TODO add your handling code here: User newUser = new User(); newUser.setId(Integer.parseInt(txtId.getText())); newUser.setFirstName(txtFirstName.getText()); newUser.setLastName(txtLastName.getText()); newUser.setEmail(txtEmail.getText()); DBObject doc = createDBObject(newUser); DB userDB = DBManager.getDatabase(); DBCollection col = userDB.getCollection("user"); WriteResult result = col.insert(doc); }//GEN-LAST:event_btnAddUserActionPerformed private void btnUpdateUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateUserActionPerformed // TODO add your handling code here: User newUser = new User(); newUser.setId(Integer.parseInt(txtId.getText())); newUser.setFirstName(txtFirstName.getText()); newUser.setLastName(txtLastName.getText()); newUser.setEmail(txtEmail.getText()); // Search BasicDBObject query = new BasicDBObject(); query.put("_id", newUser.getId()); // Set DB DB userDB = DBManager.getDatabase(); DBCollection col = userDB.getCollection("user"); // Update object DBObject doc = updateDBObject(newUser); /* // Method A // This will update all values, including all fields and values WriteResult result = col.update(query, doc); */ // Method B // Update object only with the relevant fields using $set BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", doc); WriteResult result = col.update(query, updateObj); }//GEN-LAST:event_btnUpdateUserActionPerformed /** * @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Main.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 Main().setVisible(true); } }); } private static DBObject createDBObject(User user) { BasicDBObjectBuilder docBuilder = BasicDBObjectBuilder.start(); docBuilder.append("_id", user.getId()); docBuilder.append("firstName", user.getFirstName()); docBuilder.append("lastName", user.getLastName()); docBuilder.append("email", user.getEmail()); return docBuilder.get(); } private static DBObject updateDBObject(User user) { BasicDBObjectBuilder docBuilder = BasicDBObjectBuilder.start(); docBuilder.append("firstName", user.getFirstName()); docBuilder.append("lastName", user.getLastName()); docBuilder.append("email", user.getEmail()); return docBuilder.get(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAddUser; private javax.swing.JButton btnUpdateUser; private javax.swing.JLabel lblEmail; private javax.swing.JLabel lblFirstName; private javax.swing.JLabel lblId; private javax.swing.JLabel lblLastName; private javax.swing.JTextField txtEmail; private javax.swing.JTextField txtFirstName; private javax.swing.JTextField txtId; private javax.swing.JTextField txtLastName; // End of variables declaration//GEN-END:variables }