GET Benefits

 avatar
unknown
java
2 years ago
1.5 kB
3
Indexable
import java.sql.*;

public class DealsDAO {
    
    private Connection connection;
    private PreparedStatement getDealsStatement;
    
    public DealsDAO() throws SQLException {
        // establish a database connection
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "myuser", "mypassword");
        
        // prepare a statement to retrieve all deals
        getDealsStatement = connection.prepareStatement("SELECT uuid, company, logo, url, promocode, active FROM deals");
    }
    
    public List<Deal> getAllDeals() throws SQLException {
        List<Deal> deals = new ArrayList<>();
        
        try (ResultSet resultSet = getDealsStatement.executeQuery()) {
            while (resultSet.next()) {
                // retrieve data from the result set and create a new Deal object
                String uuid = resultSet.getString("uuid");
                String company = resultSet.getString("company");
                byte[] logoBytes = resultSet.getBytes("logo");
                String url = resultSet.getString("url");
                String promocode = resultSet.getString("promocode");
                boolean active = resultSet.getBoolean("active");
                
                Deal deal = new Deal(uuid, company, logoBytes, url, promocode, active);
                deals.add(deal);
            }
        }
        
        return deals;
    }
    
    public void close() throws SQLException {
        connection.close();
    }
}
Editor is loading...