Advertisement
xlrnxnlx

DataAccess[Pilot] - working

May 21st, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. package app.dao;
  2. import app.model.Pilot;
  3. import java.sql.SQLException;
  4. import java.util.Vector;
  5.  
  6. public class PilotDataAccess extends DataAccess {
  7.    
  8.     private Pilot pilot; /* ID = BREVET... PREGUIÇA DE MUDAR :) */
  9.    
  10.     public PilotDataAccess() {}
  11.    
  12.     public PilotDataAccess( Pilot pilot ){
  13.         this.pilot = pilot;
  14.     }
  15.    
  16.     public boolean insert() {
  17.         boolean success = false;
  18.         if( connect() ){
  19.             try {
  20.                 state = con.prepareStatement("insert into pilot values(?,?,?,?,?)");
  21.                 state.setInt(1, pilot.getId());
  22.                 state.setString(2, pilot.getFname());
  23.                 state.setString(3, pilot.getLname());
  24.                 state.setDate(4, convertToSQL(pilot.getEntry()));
  25.                 state.setInt(5, pilot.getCategory().getId());
  26.                 state.executeQuery();
  27.                 success = true;
  28.             } catch (SQLException e) { }
  29.             finally {
  30.                 disconnect();
  31.             }
  32.         }
  33.         return success;
  34.     }
  35.    
  36.     public boolean delete(){
  37.         boolean success = false;
  38.         if( connect() ){
  39.             try {
  40.                 state = con.prepareStatement("delete from pilot where brevet = ?");
  41.                 state.setInt(1, pilot.getId());
  42.                 state.executeQuery();
  43.                 success = true;
  44.             } catch (SQLException e) { }
  45.             finally {
  46.                 disconnect();
  47.             }
  48.         }
  49.         return success;
  50.     }
  51.    
  52.     public boolean update(int temp) {
  53.         boolean success = false;
  54.         if( connect() ){
  55.             try {
  56.                 state = con.prepareStatement("update pilot set brevet=?, fname=?, lname=?, entry=?, pilotcategory=? where brevet = ?");
  57.                 int id = pilot.getId();
  58.                 state.setInt(1, id);
  59.                 state.setString(2, pilot.getFname());
  60.                 state.setString(3, pilot.getLname());
  61.                 state.setDate(4, convertToSQL(pilot.getEntry()));
  62.                 state.setInt(5, pilot.getCategory().getId());
  63.                 state.setInt(6, temp);
  64.                 state.executeQuery();
  65.                 success = true;
  66.             } catch( SQLException e ){ }
  67.             finally {
  68.                 disconnect();
  69.             }
  70.         }
  71.         return success;
  72.     }
  73.    
  74.     public Vector<Vector> select() {
  75.         Vector<Vector> set = null;
  76.         if( connect() ){
  77.             set = new Vector();
  78.             try {
  79.                 state = con.prepareStatement("select * from pilot");
  80.                 result = state.executeQuery();
  81.                
  82.                 Vector<String>line = null;
  83.                 while( result.next() ){
  84.                     line = new Vector();
  85.                     line.add(Integer.toString(result.getInt("brevet")));
  86.                     line.add(result.getString("fname"));
  87.                     line.add(result.getString("lname"));
  88.                     line.add(result.getDate("entry").toString());
  89.                     line.add(Integer.toString(result.getInt("pilotcategory")));
  90.                     set.add(line);
  91.                 }
  92.                
  93.             } catch (SQLException e) { }
  94.             finally {
  95.                 disconnect();
  96.             }
  97.         }
  98.         return set;
  99.     }
  100.    
  101.     public Vector<Vector> selectAllPilotNames() {
  102.         Vector<Vector> set = null;
  103.         if( connect() ){
  104.             set = new Vector();
  105.             try {
  106.                 state = con.prepareStatement("select * from pilot");
  107.                 result = state.executeQuery();
  108.                
  109.                 Vector<String>line = null;
  110.                 while( result.next() ){
  111.                     line = new Vector();
  112.                     line.add(result.getString("fname"));
  113.                     set.add(line);
  114.                 }
  115.                
  116.             } catch (SQLException e) { }
  117.             finally {
  118.                 disconnect();
  119.             }
  120.         }
  121.         return set;
  122.     }
  123.    
  124.     public String selectByID( int id ) {
  125.         String name = "";
  126.         if( connect() ){
  127.             try {
  128.                 state = con.prepareStatement("select fname from pilot where brevet = ?");
  129.                 state.setInt(1, id);
  130.                 result = state.executeQuery();
  131.                
  132.                 while( result.next() )
  133.                     name = result.getString("fname");
  134.  
  135.             } catch (SQLException e) { }
  136.             finally {
  137.                 disconnect();
  138.             }
  139.         }
  140.         return name;
  141.     }
  142.    
  143.     public int selectByName( String name ) {
  144.         int id = 0;
  145.         if( connect() ){
  146.             try {
  147.                 state = con.prepareStatement("select brevet from pilot where fname = ?");
  148.                 state.setString(1, name);
  149.                 result = state.executeQuery();
  150.                
  151.                 while( result.next() )
  152.                     id = result.getInt("brevet");
  153.  
  154.             } catch (SQLException e) { }
  155.             finally {
  156.                 disconnect();
  157.             }
  158.         }
  159.         return id;
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement