Codigo Formulario
Codigo Formulario
Codigo Formulario
*/
package form;
import java.sql.*;
import db.mysql;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
* @author Angelica - Alumnos por favor revisen los comentarios que les incluí en el codigo
*/
DefaultTableModel model;
Connection conn;
Statement sent;
/**
* Creates new form frmAlumnos
*/
initComponents();
LlenarTabla();
Deshabilitar();
InicializarBotones();
txtMat.setEnabled(true);
txtNom.setEnabled(false);
txtApe1.setEnabled(false);
txtApe2.setEnabled(false);
txtTel.setEnabled(false);
txtIng.setEnabled(false);
cmbSem.setEnabled(false);
txtGru.setEnabled(false);
txtFecNac.setEnabled(false);
cmbGen.setEnabled(false);
txtNom.setEnabled(true);
txtApe1.setEnabled(true);
txtApe2.setEnabled(true);
txtTel.setEnabled(true);
txtIng.setEnabled(true);
txtFecNac.setEnabled(true);
txtGru.setEnabled(true);
cmbSem.setEnabled(true);
cmbGen.setEnabled(true);
txtApe1.requestFocus();
txtMat.setText("");
txtNom.setText("");
txtApe1.setText("");
txtApe2.setText("");
txtTel.setText("");
txtIng.setText("");
txtGru.setText("");
txtFecNac.setText("");
cmbSem.setSelectedItem(" ");
cmbGen.setSelectedItem(" ");
btnNuevo.setEnabled(true);
btnGuardar.setEnabled(false);
btnEliminar.setEnabled(false);
btnModificar.setEnabled(false);
btnCancelar.setEnabled(true);
btnBuscar.setEnabled(true);
void LlenarTabla(){ //Función para llenar el control jTable1 con el contenido de la tabla
try{
String SQL = "SELECT * FROM alumnos"; // Ojo con el nombre de la tabla, que sea igual a la de ustedes
sent= conn.createStatement();
ResultSet rs = sent.executeQuery(SQL);
while (rs.next()){
fila[4]=rs.getString("telAlum");
fila[5]=rs.getString("fechaNac");
fila[6]=rs.getString("anioIngreso");
fila[7]=rs.getString("genero");
fila[8]=rs.getString("semestre");
fila[9]=rs.getString("grupo");
model.addRow(fila);
jTable1.setModel(model);
}catch (Exception e) {
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
}
//Función para colocar en los controles el valor seleccionado de la tabla
try{
sent=conn.createStatement();
ResultSet rs=sent.executeQuery(SQL);
rs.next();
txtMat.setEnabled(false);
txtTel.setText(rs.getString("telAlum"));
txtFecNac.setText(rs.getString("fechaNac"));
txtIng.setText(rs.getString("anioIngreso"));
cmbGen.setSelectedItem(rs.getString("genero"));
cmbSem.setSelectedItem(rs.getString("semestre"));
txtGru.setText(rs.getString("grupo"));
}catch (Exception e){
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
try{
sent= conn.createStatement();
ResultSet rs = sent.executeQuery(SQL);
while (rs.next()){
fila[1]=rs.getString("ape1Alu");
fila[2]=rs.getString("ape2Alu");
fila[3]=rs.getString("nomAlu");
fila[4]=rs.getString("telAlum");
fila[5]=rs.getString("fechaNac");
fila[6]=rs.getString("anioIngreso");
fila[7]=rs.getString("genero");
fila[8]=rs.getString("semestre");
fila[9]=rs.getString("grupo");
model.addRow(fila);
jTable1.setModel(model);
}catch(Exception e){
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
void BuscarRegistro(){ //Función para buscar en la tabla el registro introducido en matricula con el boton buscar
try{
String SQL="";
if(txtMat.getText().equals("")){
else{
int vCod=Integer.parseInt(txtMat.getText());
SQL = "SELECT * FROM Alumnos WHERE matricula="+vCod; // Ojo con el nombre de la tabla
sent=conn.createStatement();
ResultSet rs=sent.executeQuery(SQL);
rs.next();
if(rs.getRow()==0){
txtMat.setText("");
} else {
Habilitar();
ColocarValoresEnControles(SQL);
MostrarResultado(SQL);
btnNuevo.setEnabled(false);
btnGuardar.setEnabled(true);
btnEliminar.setEnabled(true);
btnModificar.setEnabled(true);
btnGuardar.setEnabled(false);
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
}
}
/**
* 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
*/
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Registro de Alumnos");
jLabel2.setText("Matricula:");
txtMat.setName(""); // NOI18N
btnNuevo.setText("Nuevo");
btnNuevo.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
NuevoMouseEncima(evt);
});
btnNuevo.addActionListener(new java.awt.event.ActionListener() {
btnNuevoActionPerformed(evt);
});
btnGuardar.setText("Guardar");
btnGuardar.addActionListener(new java.awt.event.ActionListener() {
btnGuardarActionPerformed(evt);
});
btnModificar.setText("Modificar");
btnModificar.addActionListener(new java.awt.event.ActionListener() {
btnModificarActionPerformed(evt);
});
btnEliminar.setText("Eliminar");
btnEliminar.addActionListener(new java.awt.event.ActionListener() {
btnEliminarActionPerformed(evt);
});
btnCancelar.setText("Cancelar");
btnCancelar.addActionListener(new java.awt.event.ActionListener() {
btnCancelarActionPerformed(evt);
});
jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
jLabel3.setText("Apellido Paterno");
jLabel4.setText("Apellido Materno");
jLabel5.setText("Nombre(s)");
jLabel11.setText("Genero");
jLabel8.setText("Año de Ingreso");
jLabel7.setText("Telefono");
txtFecNac.setText("AAAA-MM-DD");
jLabel6.setText("Fecha Nacimiento");
jLabel9.setText("Semestre:");
cmbSem.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " ", "1", "2", "3", "4", "5", "6" }));
jLabel10.setText("Grupo:");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel9)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel10)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(cmbGen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3))
.addComponent(jLabel4)
.addComponent(jLabel5)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(28, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addComponent(jLabel8)
.addComponent(jLabel7)
.addComponent(jLabel6)
.addComponent(jLabel11))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel10)
.addComponent(jLabel9))
);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
jTable1MouseClicked(evt);
});
jScrollPane1.setViewportView(jTable1);
btnBuscar.setText("Buscar");
btnBuscar.setName(""); // NOI18N
btnBuscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBuscarActionPerformed(evt);
});
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addComponent(btnBuscar)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addComponent(btnNuevo)
.addComponent(btnGuardar)
.addComponent(btnModificar)
.addComponent(btnEliminar)
.addComponent(btnCancelar))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jScrollPane1)
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancelar, btnEliminar, btnGuardar, btnModificar, btnNuevo});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnBuscar))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createSequentialGroup()
.addComponent(btnNuevo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnGuardar)
.addComponent(btnModificar)
.addComponent(btnEliminar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnCancelar)))
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
//OJO!!! La primera linea y las llaves de cada evento se genera desde el formulario,
Limpiar();
Deshabilitar();
InicializarBotones();
LlenarTabla();
try {
ps.setString(1, txtMat.getText());
ps.setString(2, txtApe1.getText());
ps.setString(3, txtApe2.getText());
ps.setString(4, txtNom.getText());
ps.setString(5, txtTel.getText());
ps.setString(6, txtFecNac.getText());
ps.setString(7, txtIng.getText());
ps.setString(9,(String) cmbSem.getSelectedItem());
ps.setString(10, txtGru.getText());
int n=ps.executeUpdate();
if(n>0){
Limpiar();
Deshabilitar();
InicializarBotones();
LlenarTabla();
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
btnEliminar.setEnabled(true);
btnGuardar.setEnabled(false);
btnModificar.setEnabled(true);
btnBuscar.setEnabled(false);
btnNuevo.setEnabled(false);
btnCancelar.setEnabled(true);
if(evt.getButton()==1){
try{
String SQL = "SELECT * FROM alumnos WHERE matricula="+jTable1.getValueAt(fila,0);// Ojo con el nombre de la tabla y del campo
ColocarValoresEnControles(SQL);
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
try{
String SQL = "DELETE FROM Alumnos WHERE matricula="+jTable1.getValueAt(fila,0); // Ojo con el nombre de la tabla y del campo
sent = conn.createStatement();
int n = sent.executeUpdate(SQL);
if (n>0){
Limpiar();
Deshabilitar();
InicializarBotones();
LlenarTabla();
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
BuscarRegistro();
try {
ps.setString(1, txtApe1.getText());
ps.setString(2, txtApe2.getText());
ps.setString(3, txtNom.getText());
ps.setString(4, txtTel.getText());
ps.setString(5, txtFecNac.getText());
ps.setString(6, txtIng.getText());
ps.setString(8,(String) cmbSem.getSelectedItem());
ps.setString(9, txtGru.getText());
ps.setString(10, txtMat.getText());
int n=ps.executeUpdate();
if(n>0){
Limpiar();
Deshabilitar();
InicializarBotones();
LlenarTabla();
JOptionPane.showMessageDialog(null,"ERROR:"+e.getMessage());
}
private void NuevoMouseEncima(java.awt.event.MouseEvent evt) {
Habilitar();
Limpiar();
btnNuevo.setEnabled(false);
btnGuardar.setEnabled(true);
btnEliminar.setEnabled(false);
btnModificar.setEnabled(false);
btnBuscar.setEnabled(false);
/**
*/
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new frmAlumnos().setVisible(true);
});