Advertisement
xlrnxnlx

PokemonDataAccess | Feeh/iFlash/Beats Leecher

May 18th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. package pokedex.dao;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import pokedex.model.Pokemon;
  6.  
  7. public final class PokedexDataAccess extends DataAccess {
  8.    
  9.     private Pokemon p;
  10.     private ArrayList<Pokemon> pokeList;
  11.     private ArrayList<String> pokelistname;
  12.    
  13.    
  14.     public PokedexDataAccess() { }
  15.    
  16.     public PokedexDataAccess( Pokemon p ){
  17.         this.p = p;
  18.     }
  19.     public boolean insert() {
  20.         boolean success = false;
  21.         if( connect() ){
  22.             try {
  23.                 state = con.prepareStatement("insert into pokemon values (?,?,?,?,?,?)");
  24.                 state.setInt(1, p.getID());
  25.                 state.setString(2, p.getName());
  26.                 state.setString(3, p.getType());
  27.                 state.setString(4, p.getWeak());
  28.                 state.setString(5, p.getVantage());
  29.                 state.setString(6, p.getDescription());
  30.                 state.executeQuery();
  31.                 success = true;
  32.             } catch( SQLException e ){
  33.                 System.out.println(e.getMessage());}
  34.             finally {
  35.                 disconnect();
  36.             }
  37.         }
  38.         return success;
  39.     }
  40.    
  41.     public boolean delete() {
  42.         boolean success = false;
  43.         if( connect() ){
  44.             try {
  45.                 state = con.prepareStatement("delete from pokemon where id = ?");
  46.                 state.setInt(1, p.getID());
  47.                 state.executeQuery();
  48.                 success = true;
  49.             } catch( SQLException e ) { }
  50.             finally {
  51.                 disconnect();
  52.             }
  53.         }
  54.         return success;
  55.     }
  56.    
  57.     public boolean update() {
  58.         boolean success = false;
  59.         if( connect() ){
  60.             try {
  61.                 state = con.prepareStatement("update pokemon set id=?, name=?, type=?, weak=?, vantage=?, description=? where id = ?");
  62.                 state.setInt(1, p.getID());
  63.                 state.setInt(7, p.getID());
  64.                 state.setString(2, p.getName());
  65.                 state.setString(3, p.getType());
  66.                 state.setString(4, p.getWeak());
  67.                 state.setString(5, p.getVantage());
  68.                 state.setString(6, p.getDescription());
  69.                 success = true;
  70.             } catch( SQLException e ){ }
  71.             finally {
  72.                 disconnect();
  73.             }
  74.         }
  75.         return success;
  76.     }
  77.    
  78.     public void  select() {
  79.         if(connect()){
  80.             pokeList = new ArrayList<>();
  81.             pokelistname = new ArrayList<>();
  82.             try {
  83.                
  84.                 state = con.prepareStatement("select * from pokemon");
  85.                 result = state.executeQuery();
  86.                
  87.                 while( result.next() ) {
  88.                     p = new Pokemon();
  89.                     p.setID(result.getInt("id"));
  90.                     p.setName(result.getString("name"));
  91.                     p.setType(result.getString("type"));
  92.                     p.setWeak(result.getString("weak"));
  93.                     p.setVantage(result.getString("vantage"));
  94.                     p.setDescription(result.getString("description"));
  95.                    
  96.                     pokeList.add(p);
  97.                     pokelistname.add(p.getName());
  98.                 }
  99.                 if( pokeList == null)
  100.                     System.out.println("vazia");
  101.             } catch( SQLException e) { System.out.println(e.getMessage());}
  102.             finally {
  103.                 disconnect();
  104.             }
  105.         }
  106.     }
  107.    
  108.     /*public ArrayList<String> getPokemonNames() {
  109.         ArrayList<String> pokenames = new ArrayList<>();
  110.         if( connect() ) {
  111.             try {
  112.                 state = con.prepareStatement("select name from pokemon");
  113.                 result = state.executeQuery();
  114.                 while(result.next()) {
  115.                     pokenames.add(result.getString("name"));
  116.                 }
  117.             } catch( SQLException e) { System.out.println(e.getMessage());}
  118.             finally {
  119.                 disconnect();
  120.             }
  121.         }
  122.         return pokenames;
  123.     }*/
  124.  
  125.     public ArrayList<Pokemon> getPokeList() {
  126.         return pokeList;
  127.     }
  128.  
  129.     public ArrayList<String> getPokelistname() {
  130.         return pokelistname;
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement