Advertisement
xlujiax

Aeropuerto

Jul 4th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.38 KB | None | 0 0
  1. *JFAERO [JFRAME]
  2. package aeropuerto;
  3.  
  4. import com.lap.dao.AeropuertoImpl;
  5. import com.lap.dto.AeropuertoDTO;
  6. import java.util.List;
  7.  
  8.  * @author luisgarcia
  9. public class JFAero extends javax.swing.JFrame {
  10.  
  11.     /**
  12.      * Creates new form JFAero
  13.      */
  14.     public JFAero() {
  15.         initComponents();
  16.         jTcodigo.setText("");
  17.     }
  18.  
  19.     /**
  20.      * This method is called from within the constructor to initialize the form.
  21.      * WARNING: Do NOT modify this code. The content of this method is always
  22.      * regenerated by the Form Editor.
  23.      */
  24.     @SuppressWarnings("unchecked")
  25.     private void jTnombreActionPerformed(java.awt.event.ActionEvent evt) {                                        
  26.         // TODO add your handling code here:
  27.     }                                        
  28.  
  29.     private void jBVerTodoActionPerformed(java.awt.event.ActionEvent evt) {                                          
  30.         // TODO add your handling code here:
  31.         AeropuertoImpl impl = new AeropuertoImpl();
  32.        
  33.        
  34.         List<AeropuertoDTO> aeropuertos=impl.getAllAeropuertos();
  35.        
  36.         // por eficiencia en vez de usar un String utilizamos la clase StringBuilder
  37.         StringBuilder sb= new StringBuilder();
  38.        
  39.         for(int i=0;i<aeropuertos.size();i++){
  40.             AeropuertoDTO dto = aeropuertos.get(i);
  41.            
  42.             sb.append(dto.getCodigo());
  43.             sb.append("\t");
  44.             sb.append(dto.getNombre());
  45.             sb.append("\t");
  46.             sb.append(dto.getCiudad());
  47.             sb.append("\t");
  48.             sb.append(dto.getPais());
  49.             sb.append("\n");
  50.            
  51.         }
  52.        
  53.         jTAtodo.setText(sb.toString());
  54.     }                                        
  55.  
  56.     private void jBinsertarActionPerformed(java.awt.event.ActionEvent evt) {                                          
  57.         // TODO add your handling code here:
  58.        
  59.         // 1. recuperamos los datos ingresados y los almacenamos en un DTO
  60.         String codigo = jTcodigo.getText();
  61.         String nombre = jTnombre.getText();
  62.         String ciudad = jTciudad.getText();
  63.         String pais = jTpais.getText();
  64.        
  65.         AeropuertoDTO objAeropuerto = new AeropuertoDTO();
  66.         objAeropuerto.setCodigo(codigo);
  67.         objAeropuerto.setNombre(nombre);
  68.         objAeropuerto.setCiudad(ciudad);
  69.         objAeropuerto.setPais(pais);
  70.        
  71.         // 2. invocamos el metodo insertar
  72.         AeropuertoImpl impl = new AeropuertoImpl();
  73.         int res=impl.insertaAeropuerto(objAeropuerto);
  74.        
  75.         // 2. insertamos un nuevo aeropuerto
  76.        
  77.         if(res==1){
  78.             jTAtodo.setText("registro exitoso!");
  79.         }
  80.     }                                          
  81.  
  82.     private void jBconsultarActionPerformed(java.awt.event.ActionEvent evt) {                                            
  83.         // TODO add your handling code here:
  84.        
  85.         // 1. recuperamos el codigo de aeropuerto ingresado
  86.        
  87.         String codigo = jTcodigo.getText();
  88.         System.out.println(codigo);
  89.        
  90.         // 2. invocamos el metodo getAeropuerto de nuestra clase utilitaria de acceso a base de datos
  91.         // AeropuertoImpl
  92.         AeropuertoImpl impl = new AeropuertoImpl();
  93.         AeropuertoDTO objAeropuerto =  impl.getAeropuerto(codigo);
  94.                      
  95.         // 3. Mostramos en pantalla los datos recuperados en objAeropuerto
  96.        
  97.         if (objAeropuerto.getNombre()!=null){ // si el objeto aeropuerto tiene datos
  98.            
  99.             jTnombre.setText(objAeropuerto.getNombre());
  100.             jTciudad.setText(objAeropuerto.getCiudad());
  101.             jTpais.setText(objAeropuerto.getPais());
  102.         }        
  103.                              
  104.     }                                          
  105.  
  106.     private void jBactualizarActionPerformed(java.awt.event.ActionEvent evt) {                                            
  107.         // TODO add your handling code here:
  108.         String codigo = jTcodigo.getText();
  109.         String nombre = jTnombre.getText();
  110.         String ciudad = jTciudad.getText();
  111.         String pais = jTpais.getText();
  112.        
  113.         AeropuertoDTO objAeropuerto = new AeropuertoDTO();
  114.         objAeropuerto.setCodigo(codigo);
  115.         objAeropuerto.setNombre(nombre);
  116.         objAeropuerto.setCiudad(ciudad);
  117.         objAeropuerto.setPais(pais);
  118.        
  119.         AeropuertoImpl impl = new AeropuertoImpl();
  120.        
  121.         AeropuertoDTO objAeropuerto2 = impl.getAeropuerto(codigo);
  122.        
  123.         if (objAeropuerto2.getNombre()!=null){
  124.             int res =  impl.updateAeropuerto(objAeropuerto);
  125.             if(res==1){
  126.                 jTAtodo.setText("modificación exitosa!");
  127.             }                  
  128.         }else{
  129.             jTAtodo.setText("Elemento no encontrado!");
  130.         }
  131.     }                                            
  132.  
  133.     /**
  134.      * @param args the command line arguments
  135.      */
  136.     public static void main(String args[]) {
  137.         java.awt.EventQueue.invokeLater(new Runnable() {
  138.             public void run() {
  139.                 new JFAero().setVisible(true);
  140.             }
  141.         });
  142.     }
  143. }
  144. -----
  145. *Metodos
  146. package com.lap.dao;
  147.  
  148. import java.sql.Connection;
  149. import java.sql.DriverManager;
  150. import java.sql.PreparedStatement;
  151. import java.sql.ResultSet;
  152. import java.sql.SQLException;
  153. import java.sql.Statement;
  154. import java.util.ArrayList;
  155. import java.util.List;
  156.  
  157. import com.lap.dto.AeropuertoDTO;
  158. import com.lap.dto.PanelDTO;
  159.  
  160. //public class AeropuertoImpl implements AeropuertoIF {
  161. /*
  162.  * Esta clase tiene los metodos necesario para poder  obtener una conexion
  163.  * y hacer operaciones (por ejemplo consultas)  a la
  164.  *  base de datos facilitodb, desde nuestra aplicacion java
  165.  *  
  166.  */
  167. public class AeropuertoImpl {
  168.  
  169.     Connection conn = null;
  170.  
  171.     String url
  172.             = "jdbc:mysql://localhost:3306/facilitodb?user=root&password=xlujiax147&"
  173.             + "useTimezone=true&serverTimezone=UTC";
  174.  
  175.     public AeropuertoImpl() {
  176.         // Cargar el driver
  177.         try {
  178.             Class.forName("com.mysql.cj.jdbc.Driver");
  179.         } catch (Exception e) {
  180.             System.out.println("No se pudo cargar el driver");
  181.         }
  182.     }
  183.  
  184.     private Connection getConnection() {
  185.         try {
  186.             conn = DriverManager.getConnection(url);
  187.         } catch (SQLException e) {
  188.             System.out.println("No se pudo otener conexion");
  189.             e.printStackTrace();
  190.         }
  191.         return conn;
  192.     }
  193.  
  194.     public List getAllAeropuertos() {
  195.         // Variables SQL
  196.         Statement stmt = null;
  197.         List aeropuertos = new ArrayList();
  198.  
  199.         // Crear la sentencia SQL
  200.         String sql = "SELECT * FROM tbaeropuertos ORDER BY 1 ASC";
  201.         try {
  202.             this.getConnection();
  203.             stmt = conn.createStatement();
  204.  
  205.             ResultSet rs = stmt.executeQuery(sql);
  206.             // Procesar el resultset
  207.             while (rs.next()) {
  208.                 AeropuertoDTO aero = new AeropuertoDTO();
  209.                 aero.setCodigo(rs.getString(1));
  210.                 aero.setNombre(rs.getString(2));
  211.                 aero.setCiudad(rs.getString(3));
  212.                 aero.setPais(rs.getString(4));
  213.  
  214.                 aeropuertos.add(aero);
  215.             }
  216.  
  217.             rs.close();
  218.             stmt.close();
  219.             conn.close();
  220.         } catch (SQLException e) {
  221.             // TODO Auto-generated catch block
  222.             e.printStackTrace();
  223.         }
  224.         return aeropuertos;
  225.     }
  226.  
  227.     public List getAllVuelos() {
  228.         // Variables SQL
  229.         Statement stmt = null;
  230.         List vuelos = new ArrayList();
  231.  
  232.         // Crear la sentencia SQL
  233.         String sql = "SELECT * FROM tbvuelos ORDER BY 1 ASC";
  234.         try {
  235.             this.getConnection();
  236.             stmt = conn.createStatement();
  237.  
  238.             ResultSet rs = stmt.executeQuery(sql);
  239.             // Procesar el resultset
  240.             while (rs.next()) {
  241.                 PanelDTO p = new PanelDTO();
  242.                 p.setId(rs.getInt(1));
  243.                 p.setOrigen(rs.getString(2));
  244.                 p.setDestino(rs.getString(3));
  245.                 p.setNumero(rs.getString(4));
  246.                 p.setAsientos(rs.getInt(5));
  247.  
  248.                 vuelos.add(p);
  249.             }
  250.  
  251.             rs.close();
  252.             stmt.close();
  253.             conn.close();
  254.         } catch (SQLException e) {
  255.             // TODO Auto-generated catch block
  256.             e.printStackTrace();
  257.         }
  258.         return vuelos;
  259.     }
  260.  
  261.     public String getCompania(String codigo) {
  262.         PreparedStatement stmt = null;
  263.         String nombre = "";
  264.         ResultSet rs;
  265.  
  266.         // Crear la sentencia SQL
  267.         String sql = "SELECT nombre FROM tbaerolineas WHERE LA = ?";
  268.         try {
  269.             this.getConnection();
  270.             stmt = conn.prepareStatement(sql);
  271.             stmt.setString(1, codigo);
  272.  
  273.             rs = stmt.executeQuery();
  274.  
  275.             // procesamos el resultset
  276.             while (rs.next()) {
  277.                 nombre = rs.getString(1);
  278.             }
  279.  
  280.             rs.close();
  281.             conn.close();
  282.         } catch (SQLException e) {
  283.             System.out.println("getCompania: " + e.getLocalizedMessage());
  284.             e.printStackTrace();
  285.         }
  286.         return nombre;
  287.     }
  288.  
  289.     public AeropuertoDTO getAeropuerto(String vcodigo) {
  290.  
  291.         // usamos para ejecutar insert , delete, update, select
  292.         PreparedStatement stmt = null;
  293.         ResultSet rs; // que representa en resultado de un select
  294.         AeropuertoDTO dto = new AeropuertoDTO();
  295.  
  296.         // Crear la sentencia SQL
  297.         String sql = "SELECT * FROM tbaeropuertos WHERE codigo=?";
  298.         try {
  299.             this.getConnection();
  300.             stmt = conn.prepareStatement(sql);
  301.  
  302.             // reemplazamos la interrogante 1 con vcodigo
  303.             stmt.setString(1, vcodigo);
  304.  
  305.             rs = stmt.executeQuery();
  306.  
  307.             // el resultset puede contener 0 o mas registros
  308.             while (rs.next()) {
  309.                 dto.setCodigo(rs.getString(1));
  310.                 dto.setNombre(rs.getString(2));
  311.                 dto.setCiudad(rs.getString(3));
  312.                 dto.setPais(rs.getString(4));
  313.             }
  314.             rs.close();
  315.             conn.close();
  316.         } catch (SQLException e) {
  317.             // TODO Auto-generated catch block
  318.             System.out.println("*** " + e.getLocalizedMessage());
  319.             e.printStackTrace();
  320.         }
  321.         return dto;
  322.     }
  323.  
  324.     public int insertaAeropuerto(AeropuertoDTO aeropuerto) {
  325.  
  326.         PreparedStatement stmt = null;
  327.         int res = 1;
  328.  
  329.         // Crear la sentencia SQL
  330.         String sql = "insert into tbaeropuertos values (?,?,?,?)";
  331.         try {
  332.             this.getConnection();
  333.             stmt = conn.prepareStatement(sql);
  334.             stmt.setString(1, aeropuerto.getCodigo());
  335.             stmt.setString(2, aeropuerto.getNombre());
  336.             stmt.setString(3, aeropuerto.getCiudad());
  337.             stmt.setString(4, aeropuerto.getPais());
  338.  
  339.             stmt.executeUpdate();
  340.  
  341.             conn.close();
  342.         } catch (SQLException e) {
  343.             res = 0;
  344.             // TODO Auto-generated catch block
  345.             System.out.println("*** " + e.getLocalizedMessage());
  346.             e.printStackTrace();
  347.         }
  348.         return res;
  349.     }
  350.  
  351.     public int updateAeropuerto(AeropuertoDTO aeropuerto) {
  352.         PreparedStatement stmt = null;
  353.         int res = 1;
  354.  
  355.         String sql = "update tbaeropuertos set nombre=?, ciudad=?, pais=? WHERE codigo=?";
  356.         try {
  357.             this.getConnection();
  358.             stmt = conn.prepareStatement(sql);
  359.            
  360.             stmt.setString(1, aeropuerto.getNombre());
  361.             stmt.setString(2, aeropuerto.getCiudad());
  362.             stmt.setString(3, aeropuerto.getPais());
  363.             stmt.setString(4, aeropuerto.getCodigo());
  364.  
  365.             stmt.executeUpdate();
  366.  
  367.             conn.close();
  368.         } catch (SQLException e) {
  369.             res = 0;
  370.             // TODO Auto-generated catch block
  371.             System.out.println("*** " + e.getLocalizedMessage());
  372.             e.printStackTrace();
  373.         }
  374.         return res;
  375.     }
  376. }
  377. ------------
  378. *Aeropuerto Clase
  379. package com.lap.dto;
  380.  
  381. import java.io.Serializable;
  382.  
  383. public class AeropuertoDTO implements Serializable {
  384.  
  385.     String codigo;
  386.     String nombre;
  387.     String ciudad;
  388.     String pais;
  389. ------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement