Advertisement
Guest User

Untitled

a guest
May 17th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.50 KB | None | 0 0
  1. package Caificacion;
  2.  
  3. import java.beans.*;
  4. import java.io.Serializable;
  5. import java.sql.*;
  6. import java.sql.Statement;
  7. import java.util.*;
  8. import java.sql.Date;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13. *
  14. * @author Ana Calero
  15. */
  16. public class CalificacionesBean implements Serializable {
  17. private PropertyChangeSupport propertySupport;
  18.  
  19. /*****************************************************
  20. * Propiedades del Bean.
  21. * Crearemos una propiedad por cada campo de la tabla de
  22. * la base de datos del siguiente modo:
  23. *
  24. * NIF: String
  25. * NombreDelModulo: String
  26. * AñoAcademico: String
  27. * Curso: String
  28. * Comentarios: String
  29. * NotaModulo: int
  30. */
  31. public CalificacionesBean() {
  32. propertySupport = new PropertyChangeSupport(this);
  33. try {
  34. recargarFilas();
  35. } catch (ClassNotFoundException ex) {
  36. this.NIF = "";
  37. this.NombreDelModulo = "";
  38. this.AñoAcademico = "";
  39. this.Curso= "";
  40. this.Comentarios = "";
  41. this.NotaModulo = 0;
  42.  
  43. Logger.getLogger(CalificacionesBean.class.getName()).log(Level.SEVERE, null, ex);
  44. }
  45. }
  46.  
  47. protected String NIF;
  48.  
  49. /**
  50. * Get the value of NIF
  51. *
  52. * @return the value of NIF
  53. */
  54. public String getNIF() {
  55. return NIF;
  56. }
  57.  
  58. /**
  59. * Set the value of NIF
  60. *
  61. * @param NIF new value of NIF
  62. */
  63. public void setNIF(String NIF) {
  64. this.NIF = NIF;
  65. }
  66. protected String NombreDelModulo;
  67.  
  68. /**
  69. * Get the value of NombreDelModulo
  70. *
  71. * @return the value of NombreDelModulo
  72. */
  73. public String getNombreDelModulo() {
  74. return NombreDelModulo;
  75. }
  76.  
  77. /**
  78. * Set the value of NombreDelCalificacion
  79. *
  80. * @param NombreDelCalificacion new value of NombreDelCalificacion
  81. */
  82. public void setNombreDelModulo(String NombreDelCalificacion) {
  83. this.NombreDelModulo = NombreDelModulo;
  84. }
  85.  
  86. protected String AñoAcademico;
  87.  
  88. /**
  89. * Get the value of AñoAcademico
  90. *
  91. * @return the value of AñoAcademico
  92. */
  93. public String getAñoAcademico() {
  94. return AñoAcademico;
  95. }
  96.  
  97. /**
  98. * Set the value of AñoAcademico
  99. *
  100. * @param AñoAcademico new value of AñoAcademico
  101. */
  102. public void setAñoAcademico(String AñoAcademico) {
  103. this.AñoAcademico = AñoAcademico;
  104. }
  105.  
  106. protected String Curso;
  107.  
  108. /**
  109. * Get the value of Curso
  110. *
  111. * @return the value of Curso
  112. */
  113. public String getCurso() {
  114. return Curso;
  115. }
  116.  
  117. /**
  118. * Set the value of Curso
  119. *
  120. * @param Curso new value of Curso
  121. */
  122. public void setCurso(String Curso) {
  123. this.Curso = Curso;
  124. }
  125.  
  126. protected String Comentarios;
  127.  
  128. /**
  129. * Get the value of Comentarios
  130. *
  131. * @return the value of Comentarios
  132. */
  133. public String getComentarios() {
  134. return Comentarios;
  135. }
  136.  
  137. /**
  138. * Set the value of Comentarios
  139. *
  140. * @param Comentarios new value of Comentarios
  141. */
  142. public void setComentarios(String Comentarios) {
  143. this.Comentarios = Comentarios;
  144. }
  145.  
  146. protected int NotaModulo;
  147.  
  148. /**
  149. * Get the value of NotaModulo
  150. *
  151. * @return the value of NotaModulo
  152. */
  153. public int getNotaModulo() {
  154. return NotaModulo;
  155. }
  156.  
  157. /**
  158. * Set the value of NotaModulo
  159. *
  160. * @param NotaModulo new value of NotaModulo
  161. */
  162. public void setNotaModulo(int NotaModulo) {
  163. this.NotaModulo = NotaModulo;
  164. }
  165.  
  166.  
  167. /*******************************************************
  168. * Definimos los métodos y atributos privados del
  169. * componente que usaremos para darle funcionalidad.
  170. *
  171. */
  172.  
  173.  
  174. /*****************************************************
  175. * Clase auxiliar que usaremos para crear un vector privado
  176. * de alumnos.
  177. */
  178. private class Calificacion
  179. {
  180. private String NIF;
  181. private String NombreDelModulo;
  182. private String AñoAcademico;
  183. private String Curso;
  184. private String Comentarios;
  185. private int NotaModulo;
  186.  
  187. public Calificacion()
  188. {}
  189.  
  190. public Calificacion(String nNIF, String nNombreDelModulo, String nAñoAcademico, String nCurso, String nComentarios, int nNotaModulo)
  191. {
  192. this.NIF = nNIF;
  193. this.NombreDelModulo = nNombreDelModulo;
  194. this.AñoAcademico = nAñoAcademico;
  195. this.Curso = nCurso;
  196. this.Comentarios = nComentarios;
  197. this.NotaModulo = nNotaModulo;
  198. }
  199.  
  200. /**
  201. * @return the NIF
  202. */
  203. public String getNIF() {
  204. return NIF;
  205. }
  206.  
  207. /**
  208. * @param NIF the NIF to set
  209. */
  210. public void setNIF(String NIF) {
  211. this.NIF = NIF;
  212. }
  213.  
  214. /**
  215. * @return the NombreDelModulo
  216. */
  217. public String getNombreDelModulo() {
  218. return NombreDelModulo;
  219. }
  220.  
  221. /**
  222. * @param NombreDelModulo the NombreDelModulo to set
  223. */
  224. public void setNombreDelModulo(String NombreDelModulo) {
  225. this.NombreDelModulo = NombreDelModulo;
  226. }
  227.  
  228. /**
  229. * @return the AñoAcademico
  230. */
  231. public String getAñoAcademico() {
  232. return AñoAcademico;
  233. }
  234.  
  235. /**
  236. * @param AñoAcademico the AñoAcademico to set
  237. */
  238. public void setAñoAcademico(String AñoAcademico) {
  239. this.AñoAcademico = AñoAcademico;
  240. }
  241.  
  242. /**
  243. * @return the Curso
  244. */
  245. public String getCurso() {
  246. return Curso;
  247. }
  248.  
  249. /**
  250. * @param Curso the Curso to set
  251. */
  252. public void setCurso(String Curso) {
  253. this.Curso = Curso;
  254. }
  255.  
  256. /**
  257. * @return the Comentarios
  258. */
  259. public String getComentarios() {
  260. return Comentarios;
  261. }
  262.  
  263. /**
  264. * @param Comentarios the Comentarios to set
  265. */
  266. public void setComentarios(String Comentarios) {
  267. this.Comentarios = Comentarios;
  268. }
  269.  
  270. /**
  271. * @return the NotaModulo
  272. */
  273. public int getNotaModulo() {
  274. return NotaModulo;
  275. }
  276.  
  277. /**
  278. * @param NotaModulo the NotaModulo to set
  279. */
  280. public void setNotaModulo(int NotaModulo) {
  281. this.NotaModulo = NotaModulo;
  282. }
  283. }
  284.  
  285.  
  286. /******************************************************
  287. * Usaremos un vector auxiliar para cargar la información de la
  288. * tabla de forma que tengamos acceso a los datos sin necesidad
  289. * de estar conectados constantemente
  290. */
  291. private Vector Calificaciones=new Vector();
  292.  
  293. /*******************************************************
  294. * Actualiza el contenido de la tabla en el vector de alumnos
  295. * Las propiedades contienen el valor del primer elementos de la tabla
  296. */
  297. private void recargarFilas() throws ClassNotFoundException
  298. {
  299. if(!Calificaciones.isEmpty())
  300. {
  301. Calificaciones.removeAllElements();
  302. }
  303. try {
  304. Class.forName("com.mysql.jdbc.Driver");
  305. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/calificaciones", "root", "usuario");
  306. Statement s = con.createStatement();
  307. ResultSet rs = s.executeQuery ("select * from calificaciones");
  308. while (rs.next())
  309. {
  310. Calificacion a = new Calificacion(rs.getString("NIF"),
  311. rs.getString("NombreDelModulo"),
  312. rs.getString("AñoAcademico"),
  313. rs.getString("Curso"),
  314. rs.getString("Comentarios"),
  315. rs.getInt("NotaModulo"));
  316.  
  317. Calificaciones.add(a);
  318. }
  319. Calificacion a = new Calificacion();
  320. a = (Calificacion) Calificaciones.elementAt(1);
  321. this.NIF = a.getNIF();
  322. this.NombreDelModulo = a.getNombreDelModulo();
  323. this.AñoAcademico = a.getAñoAcademico();
  324. this.Curso = a.getCurso();
  325. this.Comentarios = a.getComentarios();
  326. this.NotaModulo = a.getNotaModulo();
  327. rs.close();
  328. con.close();
  329. } catch (SQLException ex) {
  330. this.NIF = "";
  331. this.NombreDelModulo = "";
  332. this.AñoAcademico = "";
  333. this.Curso = "";
  334. this.Comentarios = "";
  335. this.NotaModulo = 0;
  336.  
  337. Logger.getLogger(CalificacionesBean.class.getName()).log(Level.SEVERE, null, ex);
  338. }
  339. }
  340. /********************************************************
  341. *
  342. * @param i numero de la fila a cargar en las propiedades del componente
  343. */
  344. public void seleccionarFila(int i)
  345. {
  346. if(i<=Calificaciones.size())
  347. {
  348. Calificacion a = new Calificacion();
  349. a = (Calificacion) Calificaciones.elementAt(i);
  350. this.NIF = a.getNIF();
  351. this.NombreDelModulo = a.getNombreDelModulo();
  352. this.AñoAcademico = a.getAñoAcademico();
  353. this.Curso = a.getCurso();
  354. this.Comentarios = a.getComentarios();
  355. this.NotaModulo = a.getNotaModulo();
  356. }else{
  357. this.NIF = "";
  358. this.NombreDelModulo = "";
  359. this.AñoAcademico = "";
  360. this.Curso = "";
  361. this.Comentarios = "";
  362. this.NotaModulo = 0;
  363. }
  364. }
  365.  
  366. /********************************************************
  367. *
  368. * @param nDNI DNI A buscar, se carga en las propiedades del componente
  369. */
  370. public void seleccionarDNI(String nNIF)
  371. {
  372. Calificacion a = new Calificacion();
  373. int i=0;
  374. this.NIF = "";
  375. this.NombreDelModulo = "";
  376. this.AñoAcademico = "";
  377. this.Curso = "";
  378. this.Comentarios = "";
  379. this.NotaModulo = 0;
  380. while(this.NIF.equals("") && i<=Calificaciones.size())
  381. {
  382. a = (Calificacion)Calificaciones.elementAt(i);
  383. if ( a.getNIF().equals(nNIF) )
  384. {
  385. this.NIF = a.getNIF();
  386. this.NombreDelModulo = a.getNombreDelModulo();
  387. this.AñoAcademico = a.getAñoAcademico();
  388. this.Curso = a.getCurso();
  389. this.Comentarios = a.getComentarios();
  390. this.NotaModulo = a.getNotaModulo();
  391. }
  392. }
  393. }
  394.  
  395.  
  396. /*********************************************************************
  397. * Código para añadir un nuevo alumno a la base de datos.
  398. * cada vez que se modifca el estado de la BD se genera un evento para
  399. * que se recargue el componente.
  400. */
  401.  
  402. private BDModificadaListener receptor;
  403.  
  404. public class BDModificadaEvent extends java.util.EventObject
  405. {
  406. // constructor
  407. public BDModificadaEvent(Object source)
  408. {
  409. super(source);
  410. }
  411. }
  412.  
  413.  
  414. //Define la interfaz para el nuevo tipo de evento
  415. public interface BDModificadaListener extends EventListener
  416. {
  417. public void capturarBDModificada(BDModificadaEvent ev);
  418. }
  419.  
  420. public void addBDModificadaListener(BDModificadaListener receptor)
  421. {
  422. this.receptor = receptor;
  423. }
  424. public void removeBDModificadaListener(BDModificadaListener receptor)
  425. {
  426. this.receptor=null;
  427. }
  428.  
  429.  
  430. /*******************************************************
  431. * Método que añade un alumno a la base de datos
  432. * añade un registro a la base de datos formado a partir
  433. * de los valores de las propiedades del componente.
  434. *
  435. * Se presupone que se han usado los métodos set para configurar
  436. * adecuadamente las propiedades con los datos del nuevo registro.
  437. */
  438. public void addCalificacion() throws ClassNotFoundException
  439. {
  440. try {
  441. Class.forName("com.mysql.jdbc.Driver");
  442. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/caificaciones", "root", "usuario");
  443. PreparedStatement s = con.prepareStatement("insert into caificaciones values (?,?,?,?,?,?)");
  444.  
  445. s.setString(1, NIF);
  446. s.setString(2, NombreDelModulo);
  447. s.setString(3, AñoAcademico);
  448. s.setString(4, Curso);
  449. s.setString(5, Comentarios);
  450. s.setInt(6, NotaModulo);
  451.  
  452. s.executeUpdate ();
  453. recargarFilas();
  454. receptor.capturarBDModificada( new BDModificadaEvent(this));
  455. }
  456. catch(SQLException ex)
  457. {
  458. Logger.getLogger(CalificacionesBean.class.getName()).log(Level.SEVERE, null, ex);
  459. }
  460.  
  461. }
  462.  
  463.  
  464.  
  465.  
  466. /*******************************************************
  467. *
  468. * @param listener
  469. */
  470.  
  471. public void addPropertyChangeListener(PropertyChangeListener listener) {
  472. propertySupport.addPropertyChangeListener(listener);
  473. }
  474.  
  475. public void removePropertyChangeListener(PropertyChangeListener listener) {
  476. propertySupport.removePropertyChangeListener(listener);
  477. }
  478.  
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement