Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. public class PokemonDA {
  2.  
  3. public final static PokemonDA instance = new PokemonDA();
  4.  
  5. private static final String URL="jdbc:mysql://localhost/pokemon";
  6. private static final String USER="root";
  7. private static final String PWD ="";
  8.  
  9. private Connection connection;
  10.  
  11. protected PokemonDA() {
  12. this.registerDriver();
  13. this.openConnection();
  14. }
  15.  
  16. private void registerDriver() {
  17. try {
  18. Class.forName("com.mysql.jdbc.Driver");
  19. } catch (ClassNotFoundException ex) {
  20. throw new PokedexException("Unable to load MySQL driver.", ex);
  21. }
  22. }
  23.  
  24. private void openConnection() {
  25. try {
  26. this.connection = DriverManager.getConnection(URL, USER, PWD);
  27. } catch (SQLException ex) {
  28. throw new PokedexException("Unable to open connection.", ex);
  29. }
  30. }
  31.  
  32. public Pokemon loadPokemon(int id) {
  33. try {
  34. String sql = "SELECT identifier FROM pokemon.pokemon WHERE id = ?;";
  35.  
  36. PreparedStatement prep = this.connection.prepareStatement(sql);
  37. prep.setInt(1, id);
  38.  
  39. Pokemon p = null;
  40. ResultSet rs = prep.executeQuery();
  41.  
  42. rs.next();
  43. p = new Pokemon(id, rs.getString("identifier"));
  44.  
  45. rs.close();
  46. prep.close();
  47. return p;
  48.  
  49. } catch (SQLException ex) {
  50. throw new PokedexException("Unable to retrieve pokemon.", ex);
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement