Java tutorial
import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; /* * 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 USER1 */ public class Modificar extends javax.swing.JFrame { DB db; DBCollection collection; Object[] columnnames; Object[] data; /** * Creates new form Inicio */ public Modificar(Object[] columnnames, Object[] data) { initComponents(); //Conexion a mongoDb try { Mongo mongo = new Mongo("localhost", 27017); db = mongo.getDB("Escuela"); collection = db.getCollection("Alumnos"); this.columnnames = columnnames; this.data = data; NombreT.setText((String) data[0]); ApPaterno.setText((String) data[1]); ApMaterno.setText((String) data[2]); dia.setSelectedItem(((String) data[3]).split("-")[0]); mes.setSelectedItem(((String) data[3]).split("-")[1]); anio.setSelectedItem(((String) data[3]).split("-")[2]); EdadT.setText((String) data[4]); sexo.setSelectedItem((String) data[5]); matricula.setText((String) data[6]); semestre.setSelectedItem((String) data[7]); Carrera.setText((String) data[8]); } catch (UnknownHostException ex) { Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex); } } /** * 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() { Nombre = new javax.swing.JLabel(); Edad = new javax.swing.JLabel(); Escuela = new javax.swing.JLabel(); NombreT = new javax.swing.JTextField(); EdadT = new javax.swing.JTextField(); ApMaterno = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); ApPaterno = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); Carrera = new javax.swing.JTextField(); semestre = new javax.swing.JComboBox<String>(); Nombre1 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); sexo = new javax.swing.JComboBox<String>(); jLabel6 = new javax.swing.JLabel(); Nombre2 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); matricula = new javax.swing.JTextField(); dia = new javax.swing.JComboBox<String>(); anio = new javax.swing.JComboBox<String>(); mes = new javax.swing.JComboBox<String>(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); Nombre.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N Nombre.setText("Nombre: "); Edad.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N Edad.setText("Apellido paterno:"); Escuela.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N Escuela.setText("Apellido materno: "); EdadT.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { EdadTActionPerformed(evt); } }); jButton1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton1.setText("Modificar"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jButton3.setText("Inicio"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(0, 102, 153)); jLabel1.setText("Insertar nuevo registro"); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel2.setText("Sexo:"); jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel3.setText("Semestre: "); jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel4.setText("Carrera:"); semestre.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" })); semestre.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { semestreActionPerformed(evt); } }); Nombre1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N Nombre1.setText("Datos personales:"); jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel5.setText("Edad: "); sexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Femenino" })); jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel6.setText("Fecha de nacimiento:"); Nombre2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N Nombre2.setText("Datos acadmicos:"); jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel7.setText("Matrcula:"); dia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30" })); dia.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { diaActionPerformed(evt); } }); anio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1980", "1981", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "2000", "2001", "2002", "2003", "2004", "2005" })); anio.setToolTipText(""); anio.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { anioActionPerformed(evt); } }); mes.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" })); mes.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { mesActionPerformed(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().addGap(41, 41, 41).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup().addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Nombre2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7) .addComponent(Nombre1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(Nombre) .addComponent(NombreT, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6) .addComponent(matricula, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(anio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(43, 43, 43) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(ApPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Edad) .addComponent(EdadT, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5).addComponent(jLabel3).addComponent( semestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(53, 53, 53) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Escuela) .addComponent(ApMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2) .addComponent(sexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Carrera, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4))))) .addGap(0, 35, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(22, 22, 22).addComponent(jLabel1).addGap(48, 48, 48) .addComponent(Nombre1).addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Nombre).addComponent(Edad).addComponent(Escuela)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(NombreT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ApPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ApMaterno, 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(jLabel2).addComponent(jLabel5).addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(EdadT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(sexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(anio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(30, 30, 30).addComponent(Nombre2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7).addComponent(jLabel3)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(semestre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(matricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(53, 53, 53) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton3)) .addGap(46, 46, 46)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(Carrera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(127, 127, 127))))); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents public List<BasicDBObject> createListBDBOfromArrays(Object[] columnnames, Object[] data) { List<BasicDBObject> obj = new ArrayList<BasicDBObject>(); if (data.length == columnnames.length) { for (int i = 0; i < data.length; i++) { if (i == 4 || i == 7) { obj.add(new BasicDBObject((String) columnnames[i], Integer.parseInt((String) data[i]))); } else obj.add(new BasicDBObject((String) columnnames[i], (String) data[i])); } } return obj; } public BasicDBObject createBDBOfromArrays(Object[] columnnames, Object[] data) { BasicDBObject obj = new BasicDBObject(); if (data.length == columnnames.length) { for (int i = 0; i < data.length; i++) { obj.append((String) columnnames[i], data[i]); } } return obj; } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed //String e = EdadT.getText(); //int i = Integer.parseInt(EdadT.getText()); if (Nombre.getText().equals("") || ApPaterno.getText().equals("") || ApMaterno.getText().equals("") || EdadT.getText().equals("") || matricula.getText().equals("") || Carrera.getText().equals("")) { JOptionPane.showMessageDialog(null, "Faltan campos por llenar"); } else { // document.put("Nombre", ""+ NombreT.getText() +""); // document.put("ApPaterno", ""+ ApPaterno.getText() +""); // document.put("ApMaterno", ""+ ApMaterno.getText() +""); // document.put("FechaNac", (String) dia.getSelectedItem() + "-" + mes.getSelectedItem() + "-" + anio.getSelectedItem()); // document.put("Edad", Integer.parseInt(EdadT.getText())); // document.put("Sexo", ""+ sexo.getSelectedItem() +""); // document.put("Matricula", ""+ matricula.getText() +""); // document.put("Semestre", Integer.parseInt(""+ semestre.getSelectedItem())); // document.put("Carrera", ""+ Carrera.getText() +""); // collection.insert(document); Object[] obj = new Object[] { NombreT.getText(), ApPaterno.getText(), ApMaterno.getText(), dia.getSelectedItem() + "-" + mes.getSelectedItem() + "-" + anio.getSelectedItem(), Integer.parseInt(EdadT.getText()), sexo.getSelectedItem(), matricula.getText(), Integer.parseInt("" + semestre.getSelectedItem()), Carrera.getText() }; BasicDBObject updateQuery = new BasicDBObject(); updateQuery.append("$set", createBDBOfromArrays(columnnames, obj)); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("$and", createListBDBOfromArrays(this.columnnames, this.data)); System.out.println(updateQuery.toString()); System.out.println(searchQuery.toString()); System.err.println(collection.update(searchQuery, updateQuery).getError()); javax.swing.JOptionPane.showMessageDialog(this, "Se ha actualizado exitosamente el alumno"); PanModificar newFrame = new PanModificar(); newFrame.setVisible(true); this.dispose(); } // TODO add your handling code here: }//GEN-LAST:event_jButton1ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed MenuPrincipal newFrame = new MenuPrincipal(); newFrame.setVisible(true); this.dispose(); // TODO add your handling code here: }//GEN-LAST:event_jButton3ActionPerformed private void EdadTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EdadTActionPerformed // TODO add your handling code here: }//GEN-LAST:event_EdadTActionPerformed private void semestreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_semestreActionPerformed // TODO add your handling code here: }//GEN-LAST:event_semestreActionPerformed private void diaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_diaActionPerformed // TODO add your handling code here: }//GEN-LAST:event_diaActionPerformed private void anioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_anioActionPerformed // TODO add your handling code here: }//GEN-LAST:event_anioActionPerformed private void mesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mesActionPerformed // TODO add your handling code here: }//GEN-LAST:event_mesActionPerformed /** * @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(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Inicio.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 Inicio().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField ApMaterno; private javax.swing.JTextField ApPaterno; private javax.swing.JTextField Carrera; private javax.swing.JLabel Edad; private javax.swing.JTextField EdadT; private javax.swing.JLabel Escuela; private javax.swing.JLabel Nombre; private javax.swing.JLabel Nombre1; private javax.swing.JLabel Nombre2; private javax.swing.JTextField NombreT; private javax.swing.JComboBox<String> anio; private javax.swing.JComboBox<String> dia; private javax.swing.JButton jButton1; private javax.swing.JButton jButton3; private javax.swing.JLabel jLabel1; 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.JTextField matricula; private javax.swing.JComboBox<String> mes; private javax.swing.JComboBox<String> semestre; private javax.swing.JComboBox<String> sexo; // End of variables declaration//GEN-END:variables }