Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *JFAERO [JFRAME]
- package aeropuerto;
- import com.lap.dao.AeropuertoImpl;
- import com.lap.dto.AeropuertoDTO;
- import java.util.List;
- * @author luisgarcia
- public class JFAero extends javax.swing.JFrame {
- /**
- * Creates new form JFAero
- */
- public JFAero() {
- initComponents();
- jTcodigo.setText("");
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- private void jTnombreActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void jBVerTodoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- AeropuertoImpl impl = new AeropuertoImpl();
- List<AeropuertoDTO> aeropuertos=impl.getAllAeropuertos();
- // por eficiencia en vez de usar un String utilizamos la clase StringBuilder
- StringBuilder sb= new StringBuilder();
- for(int i=0;i<aeropuertos.size();i++){
- AeropuertoDTO dto = aeropuertos.get(i);
- sb.append(dto.getCodigo());
- sb.append("\t");
- sb.append(dto.getNombre());
- sb.append("\t");
- sb.append(dto.getCiudad());
- sb.append("\t");
- sb.append(dto.getPais());
- sb.append("\n");
- }
- jTAtodo.setText(sb.toString());
- }
- private void jBinsertarActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- // 1. recuperamos los datos ingresados y los almacenamos en un DTO
- String codigo = jTcodigo.getText();
- String nombre = jTnombre.getText();
- String ciudad = jTciudad.getText();
- String pais = jTpais.getText();
- AeropuertoDTO objAeropuerto = new AeropuertoDTO();
- objAeropuerto.setCodigo(codigo);
- objAeropuerto.setNombre(nombre);
- objAeropuerto.setCiudad(ciudad);
- objAeropuerto.setPais(pais);
- // 2. invocamos el metodo insertar
- AeropuertoImpl impl = new AeropuertoImpl();
- int res=impl.insertaAeropuerto(objAeropuerto);
- // 2. insertamos un nuevo aeropuerto
- if(res==1){
- jTAtodo.setText("registro exitoso!");
- }
- }
- private void jBconsultarActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- // 1. recuperamos el codigo de aeropuerto ingresado
- String codigo = jTcodigo.getText();
- System.out.println(codigo);
- // 2. invocamos el metodo getAeropuerto de nuestra clase utilitaria de acceso a base de datos
- // AeropuertoImpl
- AeropuertoImpl impl = new AeropuertoImpl();
- AeropuertoDTO objAeropuerto = impl.getAeropuerto(codigo);
- // 3. Mostramos en pantalla los datos recuperados en objAeropuerto
- if (objAeropuerto.getNombre()!=null){ // si el objeto aeropuerto tiene datos
- jTnombre.setText(objAeropuerto.getNombre());
- jTciudad.setText(objAeropuerto.getCiudad());
- jTpais.setText(objAeropuerto.getPais());
- }
- }
- private void jBactualizarActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String codigo = jTcodigo.getText();
- String nombre = jTnombre.getText();
- String ciudad = jTciudad.getText();
- String pais = jTpais.getText();
- AeropuertoDTO objAeropuerto = new AeropuertoDTO();
- objAeropuerto.setCodigo(codigo);
- objAeropuerto.setNombre(nombre);
- objAeropuerto.setCiudad(ciudad);
- objAeropuerto.setPais(pais);
- AeropuertoImpl impl = new AeropuertoImpl();
- AeropuertoDTO objAeropuerto2 = impl.getAeropuerto(codigo);
- if (objAeropuerto2.getNombre()!=null){
- int res = impl.updateAeropuerto(objAeropuerto);
- if(res==1){
- jTAtodo.setText("modificación exitosa!");
- }
- }else{
- jTAtodo.setText("Elemento no encontrado!");
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new JFAero().setVisible(true);
- }
- });
- }
- }
- -----
- *Metodos
- package com.lap.dao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import com.lap.dto.AeropuertoDTO;
- import com.lap.dto.PanelDTO;
- //public class AeropuertoImpl implements AeropuertoIF {
- /*
- * Esta clase tiene los metodos necesario para poder obtener una conexion
- * y hacer operaciones (por ejemplo consultas) a la
- * base de datos facilitodb, desde nuestra aplicacion java
- *
- */
- public class AeropuertoImpl {
- Connection conn = null;
- String url
- = "jdbc:mysql://localhost:3306/facilitodb?user=root&password=xlujiax147&"
- + "useTimezone=true&serverTimezone=UTC";
- public AeropuertoImpl() {
- // Cargar el driver
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (Exception e) {
- System.out.println("No se pudo cargar el driver");
- }
- }
- private Connection getConnection() {
- try {
- conn = DriverManager.getConnection(url);
- } catch (SQLException e) {
- System.out.println("No se pudo otener conexion");
- e.printStackTrace();
- }
- return conn;
- }
- public List getAllAeropuertos() {
- // Variables SQL
- Statement stmt = null;
- List aeropuertos = new ArrayList();
- // Crear la sentencia SQL
- String sql = "SELECT * FROM tbaeropuertos ORDER BY 1 ASC";
- try {
- this.getConnection();
- stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- // Procesar el resultset
- while (rs.next()) {
- AeropuertoDTO aero = new AeropuertoDTO();
- aero.setCodigo(rs.getString(1));
- aero.setNombre(rs.getString(2));
- aero.setCiudad(rs.getString(3));
- aero.setPais(rs.getString(4));
- aeropuertos.add(aero);
- }
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return aeropuertos;
- }
- public List getAllVuelos() {
- // Variables SQL
- Statement stmt = null;
- List vuelos = new ArrayList();
- // Crear la sentencia SQL
- String sql = "SELECT * FROM tbvuelos ORDER BY 1 ASC";
- try {
- this.getConnection();
- stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- // Procesar el resultset
- while (rs.next()) {
- PanelDTO p = new PanelDTO();
- p.setId(rs.getInt(1));
- p.setOrigen(rs.getString(2));
- p.setDestino(rs.getString(3));
- p.setNumero(rs.getString(4));
- p.setAsientos(rs.getInt(5));
- vuelos.add(p);
- }
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return vuelos;
- }
- public String getCompania(String codigo) {
- PreparedStatement stmt = null;
- String nombre = "";
- ResultSet rs;
- // Crear la sentencia SQL
- String sql = "SELECT nombre FROM tbaerolineas WHERE LA = ?";
- try {
- this.getConnection();
- stmt = conn.prepareStatement(sql);
- stmt.setString(1, codigo);
- rs = stmt.executeQuery();
- // procesamos el resultset
- while (rs.next()) {
- nombre = rs.getString(1);
- }
- rs.close();
- conn.close();
- } catch (SQLException e) {
- System.out.println("getCompania: " + e.getLocalizedMessage());
- e.printStackTrace();
- }
- return nombre;
- }
- public AeropuertoDTO getAeropuerto(String vcodigo) {
- // usamos para ejecutar insert , delete, update, select
- PreparedStatement stmt = null;
- ResultSet rs; // que representa en resultado de un select
- AeropuertoDTO dto = new AeropuertoDTO();
- // Crear la sentencia SQL
- String sql = "SELECT * FROM tbaeropuertos WHERE codigo=?";
- try {
- this.getConnection();
- stmt = conn.prepareStatement(sql);
- // reemplazamos la interrogante 1 con vcodigo
- stmt.setString(1, vcodigo);
- rs = stmt.executeQuery();
- // el resultset puede contener 0 o mas registros
- while (rs.next()) {
- dto.setCodigo(rs.getString(1));
- dto.setNombre(rs.getString(2));
- dto.setCiudad(rs.getString(3));
- dto.setPais(rs.getString(4));
- }
- rs.close();
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- System.out.println("*** " + e.getLocalizedMessage());
- e.printStackTrace();
- }
- return dto;
- }
- public int insertaAeropuerto(AeropuertoDTO aeropuerto) {
- PreparedStatement stmt = null;
- int res = 1;
- // Crear la sentencia SQL
- String sql = "insert into tbaeropuertos values (?,?,?,?)";
- try {
- this.getConnection();
- stmt = conn.prepareStatement(sql);
- stmt.setString(1, aeropuerto.getCodigo());
- stmt.setString(2, aeropuerto.getNombre());
- stmt.setString(3, aeropuerto.getCiudad());
- stmt.setString(4, aeropuerto.getPais());
- stmt.executeUpdate();
- conn.close();
- } catch (SQLException e) {
- res = 0;
- // TODO Auto-generated catch block
- System.out.println("*** " + e.getLocalizedMessage());
- e.printStackTrace();
- }
- return res;
- }
- public int updateAeropuerto(AeropuertoDTO aeropuerto) {
- PreparedStatement stmt = null;
- int res = 1;
- String sql = "update tbaeropuertos set nombre=?, ciudad=?, pais=? WHERE codigo=?";
- try {
- this.getConnection();
- stmt = conn.prepareStatement(sql);
- stmt.setString(1, aeropuerto.getNombre());
- stmt.setString(2, aeropuerto.getCiudad());
- stmt.setString(3, aeropuerto.getPais());
- stmt.setString(4, aeropuerto.getCodigo());
- stmt.executeUpdate();
- conn.close();
- } catch (SQLException e) {
- res = 0;
- // TODO Auto-generated catch block
- System.out.println("*** " + e.getLocalizedMessage());
- e.printStackTrace();
- }
- return res;
- }
- }
- ------------
- *Aeropuerto Clase
- package com.lap.dto;
- import java.io.Serializable;
- public class AeropuertoDTO implements Serializable {
- String codigo;
- String nombre;
- String ciudad;
- String pais;
- ------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement