Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private String url;
- private String user;
- private String password;
- public BeerDao () {
- }
- public BeerDao(String url, String user, String password) {
- this.url = url;
- this.user = user;
- this.password = password;
- }
- public String getUrl() {
- return url; }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Beer getBeerById(int id) throws BeerException { try (Connection con = getConnection();
- PreparedStatement stmt = con
- .prepareStatement("SELECT * FROM Beers WHERE Id=?")) {
- stmt.setInt(1, id);
- try (ResultSet rs = stmt.executeQuery()) {
- if (rs.next()) {
- Beer beer = new Beer();
- beer.setId(id);
- beer.setName(rs.getString("Name"));
- beer.setPrice(rs.getFloat("Price"));
- beer.setAlcohol(rs.getFloat("Alcohol"));
- beer.setStock(rs.getInt("Stock"));
- return beer;
- } else {
- return null;
- }
- }
- } catch (SQLException e) {
- throw new BeerException(e);
- }
- }
- public void updateBeer(Beer beer) throws BeerException { try (Connection con = getConnection();
- PreparedStatement stmt = con
- .prepareStatement("UPDATE Beers SET Name=?, Price=?,
- Alcohol=?, Stock=? WHERE Id=?")) {
- stmt.setString(1, beer.getName()); stmt.setFloat(2, beer.getPrice()); stmt.setFloat(3, beer.getAlcohol()); stmt.setInt(4, beer.getStock()); stmt.setInt(5, beer.getId()); stmt.executeUpdate();
- } catch (SQLException e) {
- throw new BeerException(e);
- }
- }
- // Private helper methods
- private Connection getConnection() throws SQLException {
- return DriverManager.getConnection(url,user,password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement