Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pokedex.dao;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import pokedex.model.Pokemon;
- public final class PokedexDataAccess extends DataAccess {
- private Pokemon p;
- private ArrayList<Pokemon> pokeList;
- private ArrayList<String> pokelistname;
- public PokedexDataAccess() { }
- public PokedexDataAccess( Pokemon p ){
- this.p = p;
- }
- public boolean insert() {
- boolean success = false;
- if( connect() ){
- try {
- state = con.prepareStatement("insert into pokemon values (?,?,?,?,?,?)");
- state.setInt(1, p.getID());
- state.setString(2, p.getName());
- state.setString(3, p.getType());
- state.setString(4, p.getWeak());
- state.setString(5, p.getVantage());
- state.setString(6, p.getDescription());
- state.executeQuery();
- success = true;
- } catch( SQLException e ){
- System.out.println(e.getMessage());}
- finally {
- disconnect();
- }
- }
- return success;
- }
- public boolean delete() {
- boolean success = false;
- if( connect() ){
- try {
- state = con.prepareStatement("delete from pokemon where id = ?");
- state.setInt(1, p.getID());
- state.executeQuery();
- success = true;
- } catch( SQLException e ) { }
- finally {
- disconnect();
- }
- }
- return success;
- }
- public boolean update() {
- boolean success = false;
- if( connect() ){
- try {
- state = con.prepareStatement("update pokemon set id=?, name=?, type=?, weak=?, vantage=?, description=? where id = ?");
- state.setInt(1, p.getID());
- state.setInt(7, p.getID());
- state.setString(2, p.getName());
- state.setString(3, p.getType());
- state.setString(4, p.getWeak());
- state.setString(5, p.getVantage());
- state.setString(6, p.getDescription());
- success = true;
- } catch( SQLException e ){ }
- finally {
- disconnect();
- }
- }
- return success;
- }
- public void select() {
- if(connect()){
- pokeList = new ArrayList<>();
- pokelistname = new ArrayList<>();
- try {
- state = con.prepareStatement("select * from pokemon");
- result = state.executeQuery();
- while( result.next() ) {
- p = new Pokemon();
- p.setID(result.getInt("id"));
- p.setName(result.getString("name"));
- p.setType(result.getString("type"));
- p.setWeak(result.getString("weak"));
- p.setVantage(result.getString("vantage"));
- p.setDescription(result.getString("description"));
- pokeList.add(p);
- pokelistname.add(p.getName());
- }
- if( pokeList == null)
- System.out.println("vazia");
- } catch( SQLException e) { System.out.println(e.getMessage());}
- finally {
- disconnect();
- }
- }
- }
- /*public ArrayList<String> getPokemonNames() {
- ArrayList<String> pokenames = new ArrayList<>();
- if( connect() ) {
- try {
- state = con.prepareStatement("select name from pokemon");
- result = state.executeQuery();
- while(result.next()) {
- pokenames.add(result.getString("name"));
- }
- } catch( SQLException e) { System.out.println(e.getMessage());}
- finally {
- disconnect();
- }
- }
- return pokenames;
- }*/
- public ArrayList<Pokemon> getPokeList() {
- return pokeList;
- }
- public ArrayList<String> getPokelistname() {
- return pokelistname;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement