You are on page 1of 3

PROGRAMACION CON BASE DE DATOS

/* * To change this template, choose Tools | Templates * Y el claro la plantilla en el editor. */ package Biblioteca; import java.sql.*; import javax.swing.JOptionPane; /** * * @author Rafael */ public class Cliente extends javax.swing.JFrame { private Connection con; private Statement st; private ResultSet rs; int n,m=0; /** * Crea UN Nuevo formulario Clientes */ public Cliente() { initComponents(); try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca", "root", "rocio"); st=con.createStatement(rs.TYPE_SCROLL_SENSITIVE, rs.CONCUR_READ_ONLY); } catch(ClassNotFoundException e) { JOptionPane.showMessageDialog(null,e.getMessage(),"Error en el JDBC",JOptionPane.ERROR_MESSAGE);

controlador

} catch(SQLException e) { JOptionPane.showMessageDialog(null,e.getMessage(),"Error Datos",JOptionPane.ERROR_MESSAGE); } } private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); }

en

la

Base

de

private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) { if (m == 1) { try { String modifica = ""; modifica = "UPDATE Alumno SET Alumno='" + txtCod_Alumno.getText().toString() + "',ape_alumno='" + txtApe_Alumno.getText().toString() + "',nom_alumno='" + txtNom_Alumno.getText().toString() + "',edad='" + txtEdad.getText().toString() + "' WHERE Cod_Alumno='" + txtCod_Alumno.getText().toString() + "'"; st.execute(modifica); } catch (SQLException ex) { } } else { if (n == 1) { try { String nuevo = ""; nuevo = "INSERT INTO Alumno VALUES('" + txtCod_Alumno.getText().toString() + "','" + txtNom_Alumno.getText().toString() + "','" + txtApe_Alumno.getText().toString() + "','" + txtEdad.getText().toString() + "')"; st.execute(nuevo); } catch (SQLException ex) { } } deshabilitaText(); } private void deshabilitaText()

{ txtCod_Alumno.setEnabled(false); txtNom_Alumno.setEnabled(false); txtApe_Alumno.setEnabled(false); txtEdad.setEnabled(false); } private void habilitaText() { txtCod_Alumno.setEnabled(true); txtNom_Alumno.setEnabled(true); txtApe_Alumno.setEnabled(true); txtEdad.setEnabled(true); } private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) { deshabilitaText(); } private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { n = 1; txtNom_Alumno.setText(""); txtApe_Alumno.setText(""); txtCod_Alumno.setText(""); txtEdad.setText(""); txtCod_Alumno.grabFocus(); } private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) { m = 1; habilitaText(); }

You might also like