Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PokemonDA {
- public final static PokemonDA instance = new PokemonDA();
- private static final String URL="jdbc:mysql://localhost/pokemon";
- private static final String USER="root";
- private static final String PWD ="";
- private Connection connection;
- protected PokemonDA() {
- this.registerDriver();
- this.openConnection();
- }
- private void registerDriver() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- throw new PokedexException("Unable to load MySQL driver.", ex);
- }
- }
- private void openConnection() {
- try {
- this.connection = DriverManager.getConnection(URL, USER, PWD);
- } catch (SQLException ex) {
- throw new PokedexException("Unable to open connection.", ex);
- }
- }
- public Pokemon loadPokemon(int id) {
- try {
- String sql = "SELECT identifier FROM pokemon.pokemon WHERE id = ?;";
- PreparedStatement prep = this.connection.prepareStatement(sql);
- prep.setInt(1, id);
- Pokemon p = null;
- ResultSet rs = prep.executeQuery();
- rs.next();
- p = new Pokemon(id, rs.getString("identifier"));
- rs.close();
- prep.close();
- return p;
- } catch (SQLException ex) {
- throw new PokedexException("Unable to retrieve pokemon.", ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement