Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Caificacion;
- import java.beans.*;
- import java.io.Serializable;
- import java.sql.*;
- import java.sql.Statement;
- import java.util.*;
- import java.sql.Date;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Ana Calero
- */
- public class CalificacionesBean implements Serializable {
- private PropertyChangeSupport propertySupport;
- /*****************************************************
- * Propiedades del Bean.
- * Crearemos una propiedad por cada campo de la tabla de
- * la base de datos del siguiente modo:
- *
- * NIF: String
- * NombreDelModulo: String
- * AñoAcademico: String
- * Curso: String
- * Comentarios: String
- * NotaModulo: int
- */
- public CalificacionesBean() {
- propertySupport = new PropertyChangeSupport(this);
- try {
- recargarFilas();
- } catch (ClassNotFoundException ex) {
- this.NIF = "";
- this.NombreDelModulo = "";
- this.AñoAcademico = "";
- this.Curso= "";
- this.Comentarios = "";
- this.NotaModulo = 0;
- Logger.getLogger(CalificacionesBean.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- protected String NIF;
- /**
- * Get the value of NIF
- *
- * @return the value of NIF
- */
- public String getNIF() {
- return NIF;
- }
- /**
- * Set the value of NIF
- *
- * @param NIF new value of NIF
- */
- public void setNIF(String NIF) {
- this.NIF = NIF;
- }
- protected String NombreDelModulo;
- /**
- * Get the value of NombreDelModulo
- *
- * @return the value of NombreDelModulo
- */
- public String getNombreDelModulo() {
- return NombreDelModulo;
- }
- /**
- * Set the value of NombreDelCalificacion
- *
- * @param NombreDelCalificacion new value of NombreDelCalificacion
- */
- public void setNombreDelModulo(String NombreDelCalificacion) {
- this.NombreDelModulo = NombreDelModulo;
- }
- protected String AñoAcademico;
- /**
- * Get the value of AñoAcademico
- *
- * @return the value of AñoAcademico
- */
- public String getAñoAcademico() {
- return AñoAcademico;
- }
- /**
- * Set the value of AñoAcademico
- *
- * @param AñoAcademico new value of AñoAcademico
- */
- public void setAñoAcademico(String AñoAcademico) {
- this.AñoAcademico = AñoAcademico;
- }
- protected String Curso;
- /**
- * Get the value of Curso
- *
- * @return the value of Curso
- */
- public String getCurso() {
- return Curso;
- }
- /**
- * Set the value of Curso
- *
- * @param Curso new value of Curso
- */
- public void setCurso(String Curso) {
- this.Curso = Curso;
- }
- protected String Comentarios;
- /**
- * Get the value of Comentarios
- *
- * @return the value of Comentarios
- */
- public String getComentarios() {
- return Comentarios;
- }
- /**
- * Set the value of Comentarios
- *
- * @param Comentarios new value of Comentarios
- */
- public void setComentarios(String Comentarios) {
- this.Comentarios = Comentarios;
- }
- protected int NotaModulo;
- /**
- * Get the value of NotaModulo
- *
- * @return the value of NotaModulo
- */
- public int getNotaModulo() {
- return NotaModulo;
- }
- /**
- * Set the value of NotaModulo
- *
- * @param NotaModulo new value of NotaModulo
- */
- public void setNotaModulo(int NotaModulo) {
- this.NotaModulo = NotaModulo;
- }
- /*******************************************************
- * Definimos los métodos y atributos privados del
- * componente que usaremos para darle funcionalidad.
- *
- */
- /*****************************************************
- * Clase auxiliar que usaremos para crear un vector privado
- * de alumnos.
- */
- private class Calificacion
- {
- private String NIF;
- private String NombreDelModulo;
- private String AñoAcademico;
- private String Curso;
- private String Comentarios;
- private int NotaModulo;
- public Calificacion()
- {}
- public Calificacion(String nNIF, String nNombreDelModulo, String nAñoAcademico, String nCurso, String nComentarios, int nNotaModulo)
- {
- this.NIF = nNIF;
- this.NombreDelModulo = nNombreDelModulo;
- this.AñoAcademico = nAñoAcademico;
- this.Curso = nCurso;
- this.Comentarios = nComentarios;
- this.NotaModulo = nNotaModulo;
- }
- /**
- * @return the NIF
- */
- public String getNIF() {
- return NIF;
- }
- /**
- * @param NIF the NIF to set
- */
- public void setNIF(String NIF) {
- this.NIF = NIF;
- }
- /**
- * @return the NombreDelModulo
- */
- public String getNombreDelModulo() {
- return NombreDelModulo;
- }
- /**
- * @param NombreDelModulo the NombreDelModulo to set
- */
- public void setNombreDelModulo(String NombreDelModulo) {
- this.NombreDelModulo = NombreDelModulo;
- }
- /**
- * @return the AñoAcademico
- */
- public String getAñoAcademico() {
- return AñoAcademico;
- }
- /**
- * @param AñoAcademico the AñoAcademico to set
- */
- public void setAñoAcademico(String AñoAcademico) {
- this.AñoAcademico = AñoAcademico;
- }
- /**
- * @return the Curso
- */
- public String getCurso() {
- return Curso;
- }
- /**
- * @param Curso the Curso to set
- */
- public void setCurso(String Curso) {
- this.Curso = Curso;
- }
- /**
- * @return the Comentarios
- */
- public String getComentarios() {
- return Comentarios;
- }
- /**
- * @param Comentarios the Comentarios to set
- */
- public void setComentarios(String Comentarios) {
- this.Comentarios = Comentarios;
- }
- /**
- * @return the NotaModulo
- */
- public int getNotaModulo() {
- return NotaModulo;
- }
- /**
- * @param NotaModulo the NotaModulo to set
- */
- public void setNotaModulo(int NotaModulo) {
- this.NotaModulo = NotaModulo;
- }
- }
- /******************************************************
- * Usaremos un vector auxiliar para cargar la información de la
- * tabla de forma que tengamos acceso a los datos sin necesidad
- * de estar conectados constantemente
- */
- private Vector Calificaciones=new Vector();
- /*******************************************************
- * Actualiza el contenido de la tabla en el vector de alumnos
- * Las propiedades contienen el valor del primer elementos de la tabla
- */
- private void recargarFilas() throws ClassNotFoundException
- {
- if(!Calificaciones.isEmpty())
- {
- Calificaciones.removeAllElements();
- }
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost/calificaciones", "root", "usuario");
- Statement s = con.createStatement();
- ResultSet rs = s.executeQuery ("select * from calificaciones");
- while (rs.next())
- {
- Calificacion a = new Calificacion(rs.getString("NIF"),
- rs.getString("NombreDelModulo"),
- rs.getString("AñoAcademico"),
- rs.getString("Curso"),
- rs.getString("Comentarios"),
- rs.getInt("NotaModulo"));
- Calificaciones.add(a);
- }
- Calificacion a = new Calificacion();
- a = (Calificacion) Calificaciones.elementAt(1);
- this.NIF = a.getNIF();
- this.NombreDelModulo = a.getNombreDelModulo();
- this.AñoAcademico = a.getAñoAcademico();
- this.Curso = a.getCurso();
- this.Comentarios = a.getComentarios();
- this.NotaModulo = a.getNotaModulo();
- rs.close();
- con.close();
- } catch (SQLException ex) {
- this.NIF = "";
- this.NombreDelModulo = "";
- this.AñoAcademico = "";
- this.Curso = "";
- this.Comentarios = "";
- this.NotaModulo = 0;
- Logger.getLogger(CalificacionesBean.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /********************************************************
- *
- * @param i numero de la fila a cargar en las propiedades del componente
- */
- public void seleccionarFila(int i)
- {
- if(i<=Calificaciones.size())
- {
- Calificacion a = new Calificacion();
- a = (Calificacion) Calificaciones.elementAt(i);
- this.NIF = a.getNIF();
- this.NombreDelModulo = a.getNombreDelModulo();
- this.AñoAcademico = a.getAñoAcademico();
- this.Curso = a.getCurso();
- this.Comentarios = a.getComentarios();
- this.NotaModulo = a.getNotaModulo();
- }else{
- this.NIF = "";
- this.NombreDelModulo = "";
- this.AñoAcademico = "";
- this.Curso = "";
- this.Comentarios = "";
- this.NotaModulo = 0;
- }
- }
- /********************************************************
- *
- * @param nDNI DNI A buscar, se carga en las propiedades del componente
- */
- public void seleccionarDNI(String nNIF)
- {
- Calificacion a = new Calificacion();
- int i=0;
- this.NIF = "";
- this.NombreDelModulo = "";
- this.AñoAcademico = "";
- this.Curso = "";
- this.Comentarios = "";
- this.NotaModulo = 0;
- while(this.NIF.equals("") && i<=Calificaciones.size())
- {
- a = (Calificacion)Calificaciones.elementAt(i);
- if ( a.getNIF().equals(nNIF) )
- {
- this.NIF = a.getNIF();
- this.NombreDelModulo = a.getNombreDelModulo();
- this.AñoAcademico = a.getAñoAcademico();
- this.Curso = a.getCurso();
- this.Comentarios = a.getComentarios();
- this.NotaModulo = a.getNotaModulo();
- }
- }
- }
- /*********************************************************************
- * Código para añadir un nuevo alumno a la base de datos.
- * cada vez que se modifca el estado de la BD se genera un evento para
- * que se recargue el componente.
- */
- private BDModificadaListener receptor;
- public class BDModificadaEvent extends java.util.EventObject
- {
- // constructor
- public BDModificadaEvent(Object source)
- {
- super(source);
- }
- }
- //Define la interfaz para el nuevo tipo de evento
- public interface BDModificadaListener extends EventListener
- {
- public void capturarBDModificada(BDModificadaEvent ev);
- }
- public void addBDModificadaListener(BDModificadaListener receptor)
- {
- this.receptor = receptor;
- }
- public void removeBDModificadaListener(BDModificadaListener receptor)
- {
- this.receptor=null;
- }
- /*******************************************************
- * Método que añade un alumno a la base de datos
- * añade un registro a la base de datos formado a partir
- * de los valores de las propiedades del componente.
- *
- * Se presupone que se han usado los métodos set para configurar
- * adecuadamente las propiedades con los datos del nuevo registro.
- */
- public void addCalificacion() throws ClassNotFoundException
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost/caificaciones", "root", "usuario");
- PreparedStatement s = con.prepareStatement("insert into caificaciones values (?,?,?,?,?,?)");
- s.setString(1, NIF);
- s.setString(2, NombreDelModulo);
- s.setString(3, AñoAcademico);
- s.setString(4, Curso);
- s.setString(5, Comentarios);
- s.setInt(6, NotaModulo);
- s.executeUpdate ();
- recargarFilas();
- receptor.capturarBDModificada( new BDModificadaEvent(this));
- }
- catch(SQLException ex)
- {
- Logger.getLogger(CalificacionesBean.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /*******************************************************
- *
- * @param listener
- */
- public void addPropertyChangeListener(PropertyChangeListener listener) {
- propertySupport.addPropertyChangeListener(listener);
- }
- public void removePropertyChangeListener(PropertyChangeListener listener) {
- propertySupport.removePropertyChangeListener(listener);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement