You are on page 1of 7

package prueba_conexion;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Jorge
*/
public class IngresoAlumnos extends javax.swing.JFrame {
/**
* Creates new form IngresoAlumnos
*/
public IngresoAlumnos() {
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">

private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jTNombre = new javax.swing.JTextField();
jTApellido = new javax.swing.JTextField();
jTDireccion = new javax.swing.JTextField();
jTTelefono = new javax.swing.JTextField();
jBNuevo = new javax.swing.JButton();
jBRegistrar = new javax.swing.JButton();
JBSalir = new javax.swing.JButton();
JBConsulta = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("INGRESO DATOS DE ALUMNOS");
jLabel2.setText("NOMBRE");
jLabel3.setText("APELLIDOS");
jLabel4.setText("DIRECCION");
jLabel5.setText("TELEFONO");
jBNuevo.setText("NUEVO");
jBNuevo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBNuevoActionPerformed(evt);
}
});
jBRegistrar.setText("REGISTRAR");
jBRegistrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBRegistrarActionPerformed(evt);
}
});
JBSalir.setText("SALIR");
JBSalir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JBSalirActionPerformed(evt);
}
});
JBConsulta.setText("Consulta");
JBConsulta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JBConsultaActionPerformed(evt);
}
});
jButton1.setText("MODIFICAR");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP
ane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLa
yout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing
.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing
.GroupLayout.Alignment.LEADING, false)
.addComponent(jTNombre)
.addComponent(jTApellido)
.addComponent(jTDireccion)
.addComponent(jTTelefono, javax.swing.GroupL
ayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jBNuevo)
.addGap(44, 44, 44)
.addGroup(layout.createParallelGroup(javax.swing
.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(JBConsulta)
.addGap(46, 46, 46)
.addComponent(jButton1)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jBRegistrar)
.addPreferredGap(javax.swing.LayoutStyle
.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VAL
UE)
.addComponent(JBSalir)))))))
.addContainerGap(44, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTNombre, javax.swing.GroupLayout.PREFERRED_SI
ZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE
))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTApellido, javax.swing.GroupLayout.PREFERRED_
SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SI
ZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE
LATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addComponent(jLabel4)
.addComponent(jTDireccion, javax.swing.GroupLayout.PREFERRED
_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_S
IZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA
TED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.LEADING)
.addComponent(jLabel5)
.addComponent(jTTelefono, javax.swing.GroupLayout.PREFERRED_
SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SI
ZE))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(jBNuevo)
.addComponent(jBRegistrar)
.addComponent(JBSalir))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali
gnment.BASELINE)
.addComponent(JBConsulta)
.addComponent(jButton1))
.addContainerGap(48, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private final String sqlInsert =
"INSERT INTO alumno (nombre, apellidos, direccion, telefono) VALUES
(?, ?, ?, ?)";
private final String sqlUpdate =
"UPDATE alumno SET nombre = ?, apellidos = ?, direccion = ?, telefon
o = ? WHERE id = ?";
private final String sqlSelect =
"SELECT * from alumno WHERE id = ?";
public int insert(){
conectar con = new conectar();
Connection reg = con.conexion();
String vNombre, vApellido, vDireccion, vTelefono;
int n = 0; //registros afectados
vNombre = jTNombre.getText();
vApellido = jTApellido.getText();
vDireccion = jTDireccion.getText();
vTelefono = jTTelefono.getText();
try {
PreparedStatement pst = reg.prepareStatement(sqlInsert);
pst.setString(1, vNombre);
pst.setString(2, vApellido);
pst.setString(3, vDireccion);
pst.setString(4, vTelefono);
n = pst.executeUpdate();
if (n > 0) {
JOptionPane.showMessageDialog(null, "Registrado con xito");
}
else {
JOptionPane.showMessageDialog(null, "Error en el registro");

}
} catch (SQLException ex) {
Logger.getLogger(IngresoAlumnos.class.getName()).log(Level.SEVERE, n
ull, ex);
}
/*
finally {
conectar;
conectar.close(con);
*/
return n;
}

public int update (int pid, String pnombre, String papellidos, String pdirec
cion, String ptelefono) {
conectar con = new conectar();
Connection reg = con.conexion();
int n = 0;
try {
PreparedStatement pst = reg.prepareStatement(sqlUpdate);
pst.setString(1, pnombre);
pst.setString(2, papellidos);
pst.setString(3, pdireccion);
pst.setString(4, ptelefono);
pst.setInt(5, pid);
n = pst.executeUpdate();
if (n > 0) {
JOptionPane.showMessageDialog(null, "Registros afectados " + n);
}
else {
JOptionPane.showMessageDialog(null, "Error en la actualizacin");

}


} catch (SQLException ex) {
Logger.getLogger(IngresoAlumnos.class.getName()).log(Level.SEVERE, n
ull, ex);
}
return n;
}

public void select (int pid) {
conectar con = new conectar();
Connection reg = con.conexion();
try {
PreparedStatement pst = reg.prepareStatement(sqlSelect);
//ResultSet rs = null;
pst.setInt(1, pid);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
int vid = rs.getInt(1);
String vnombre = rs.getString(2);
String vapellidos = rs.getString(3);
String vdireccion = rs.getString(4);
String vtelefono = rs.getString(5);
System.out.println("Id: "+vid);
System.out.println("Nombre: "+vnombre);
System.out.println("Apellidos: "+vapellidos);
System.out.println("Direccin: "+vdireccion);
System.out.println("Telfono: "+vtelefono);
}

} catch (SQLException ex) {
Logger.getLogger(IngresoAlumnos.class.getName()).log(Level.SEVERE, n
ull, ex);
}
}
private void jBRegistrarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:
insert();
}
private void jBNuevoActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:
jTNombre.setText("");
jTApellido.setText("");
jTDireccion.setText("");
jTTelefono.setText("");
}
private void JBSalirActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:
System.exit(0);
}
private void JBConsultaActionPerformed(java.awt.event.ActionEvent evt) {



// TODO add your handling code here:
select(1);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:
update(1, "NEW NOMBRE", "NEW APELLIDOS", "NEW DIRECCION", "NEW TELEFONO"
);
}
/**
* @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 cod
e (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the d
efault look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lo
okandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIMana
ger.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(IngresoAlumnos.class.getName()).l
og(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(IngresoAlumnos.class.getName()).l
og(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(IngresoAlumnos.class.getName()).l
og(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(IngresoAlumnos.class.getName()).l
og(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new IngresoAlumnos().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton JBConsulta;
private javax.swing.JButton JBSalir;
private javax.swing.JButton jBNuevo;
private javax.swing.JButton jBRegistrar;
private javax.swing.JButton jButton1;
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.JTextField jTApellido;
private javax.swing.JTextField jTDireccion;
private javax.swing.JTextField jTNombre;
private javax.swing.JTextField jTTelefono;
// End of variables declaration
}

You might also like