Advertisement
xEzee

DocenteBean

Nov 13th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. import java.sql.SQLException;
  2.  
  3. import javax.sql.rowset.JdbcRowSet;
  4. import javax.swing.JOptionPane;
  5.  
  6. import com.sun.rowset.JdbcRowSetImpl;
  7.  
  8. //Interacción con la tabla
  9. public class DocenteBean {
  10.  
  11.     // Driver JDBC, url para la conexión, usuario y pass
  12.     static final String JDBC_DRIVER = "org.firebirdsql.jdbc.FBDriver";
  13.     static final String DB_URL = "jdbc:firebirdsql:localhost/3050:C:\\EXAMENES.FDB";
  14.     static final String DB_USER = "SYSDBA";
  15.     static final String DB_PASS = "masterkey";
  16.     // Objeto que contiene los datos de la tabla. (Interfaz)
  17.     private JdbcRowSet rowSet = null;
  18.  
  19.     // Constructor de la clase
  20.     public DocenteBean() {
  21.         try {
  22.             Class.forName(JDBC_DRIVER); // Carga del driver
  23.             rowSet = new JdbcRowSetImpl();
  24.             rowSet.setUrl(DB_URL);
  25.             rowSet.setUsername(DB_USER);
  26.             rowSet.setPassword(DB_PASS);
  27.             rowSet.setCommand("SELECT * FROM DOCENTE");
  28.             rowSet.execute();
  29.  
  30.         } catch (SQLException | ClassNotFoundException ex) {
  31.             ex.printStackTrace();
  32.         }
  33.     }
  34.  
  35.     // docente es la instancia de Docente creada
  36.     public Docente crear(Docente docente) {
  37.         try {
  38.             rowSet.moveToInsertRow(); // Muevo a la fila que vamos a insertar
  39.             rowSet.updateInt("dni", docente.getDni());
  40.             rowSet.updateString("estado", docente.getEstado());
  41.             rowSet.insertRow(); // Acá se inserta un registro en la tabla
  42.             rowSet.moveToCurrentRow();// Mueve al registro actual
  43.  
  44.         } catch (SQLException ex) {
  45.             try {
  46.                 rowSet.rollback(); // rollback > anula los cambios realizados
  47.                 docente = null;
  48.             } catch (SQLException e) {
  49.  
  50.             }
  51.             ex.printStackTrace();
  52.         }
  53.         return docente;
  54.  
  55.     }
  56.  
  57.     // La instancia del Docente con los datos actualizados en la base de datos
  58.     public Docente actualizar(Docente docente) {
  59.         try {
  60.             // Actualiza los datos del registro actual
  61.             rowSet.updateString("estado", docente.getEstado());
  62.             rowSet.updateRow();// Actualiza el registro en la tabla
  63.             rowSet.moveToCurrentRow(); // Mueve al registro actual
  64.         } catch (SQLException ex) {
  65.             try {
  66.                 rowSet.rollback();
  67.             } catch (SQLException e) {
  68.  
  69.             }
  70.             ex.printStackTrace();
  71.         }
  72.         return docente;
  73.  
  74.     }
  75.  
  76.     // Eliminar registro actual de la Tabla
  77.     public void borrar() {
  78.         try {
  79.             rowSet.moveToCurrentRow(); // Mueve al registro actual
  80.             rowSet.deleteRow(); // Elimina el reg de la BD
  81.         } catch (SQLException ex) {
  82.             try {
  83.                 rowSet.rollback();
  84.             } catch (SQLException e) {
  85.             }
  86.             ex.printStackTrace();
  87.         }
  88.  
  89.     }
  90.  
  91.     // @return instancia de docente con los datos del registo (mueveAlPrimero
  92.     public Docente mueveAlPrimero() {
  93.         Docente docente = new Docente();
  94.                 try{
  95.                     rowSet.first(); // Muevo el cursor a la primer fila y cargo los
  96.                             // datos
  97.                     docente.setDni(rowSet.getInt("dni"));
  98.                     docente.setEstado(rowSet.getString("estado"));
  99.                 } catch (SQLException ex) {
  100.                     ex.printStackTrace();
  101.                 }
  102.                 return docente;
  103.     }
  104.  
  105.  
  106.     // @return instancia de docente con los datos del registro (mueveAlUltimo)
  107.     public Docente mueveAlUltimo() {
  108.         Docente docente = new Docente();
  109.                 try {
  110.                     rowSet.last();
  111.                     docente.setDni(rowSet.getInt("dni"));
  112.                     docente.setEstado(rowSet.getString("estado"));
  113.                 } catch (SQLException ex) {
  114.                     ex.printStackTrace();
  115.                 }
  116.                 return docente;
  117.             }
  118.            
  119.     // @return instancia de docente con los datos del registro (mueveAlProximo)
  120.     public Docente mueveAlProximo() {
  121.         Docente docente = new Docente();
  122.         try {
  123.             if (rowSet.next() == false)
  124.                 rowSet.previous();
  125.             docente.setDni(rowSet.getInt("dni"));
  126.             docente.setEstado(rowSet.getString("estado"));
  127.  
  128.         } catch (SQLException ex) {
  129.             ex.printStackTrace();
  130.         }
  131.         return docente;
  132.     }
  133.  
  134.     // @return instancia de personas con los datos del registro
  135.     // (mueveAlAnterior)
  136.     public Docente mueveAlAnterior() {
  137.         Docente docente = new Docente();
  138.         try {
  139.             if (rowSet.previous() == false)
  140.                 rowSet.next();
  141.             docente.setDni(rowSet.getInt("dni"));
  142.             docente.setEstado(rowSet.getString("estado"));
  143.  
  144.         } catch (SQLException ex) {
  145.             ex.printStackTrace();
  146.         }
  147.         return docente;
  148.  
  149.     }
  150.  
  151.     // @return instancia de docente con los datos del registro
  152.     public Docente getDatosRegistro() {
  153.         Docente docente = new Docente();
  154.         try {
  155.             rowSet.moveToCurrentRow();
  156.             docente.setDni(rowSet.getInt("dni"));
  157.             docente.setEstado(rowSet.getString("estado"));
  158.  
  159.         } catch (SQLException ex) {
  160.             ex.printStackTrace();
  161.         }
  162.         return docente;
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement