Sum in Java JDBC

This project will demonstrate how to Use Sum in Java JDBC

  • LearnJavaJDBC
    • src
      • demo
        • Main.java
      • entities
        • Product.java
      • models
        • ProductModel.java
        • ConnectDatabase.java
    • pom.xml
package entities;

import java.math.BigDecimal;
import java.util.Date;

public class Product implements java.io.Serializable {

    private int id;
    private String name;
    private BigDecimal price;
    private int quantity;
    private String description;
    private boolean featured;
    private Date dateCreated;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isFeatured() {
        return featured;
    }

    public void setFeatured(boolean featured) {
        this.featured = featured;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

}
package models;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ProductModel {

    public int sumQuantities() {
        int result = 0;
        try {
            PreparedStatement preparedStatement = ConnectDatabase.getConnection().prepareStatement("select sum(quantity) from product");
            ResultSet resultSet = preparedStatement.executeQuery();
            resultSet.next();
            result = resultSet.getInt(1);
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }

    public BigDecimal total() {
        BigDecimal result = null;
        try {
            PreparedStatement preparedStatement = ConnectDatabase.getConnection().prepareStatement("select sum(price * quantity) from product");
            ResultSet resultSet = preparedStatement.executeQuery();
            resultSet.next();
            result = resultSet.getBigDecimal(1);
        } catch (Exception e) {
            result = null;
        }
        return result;
    }

}
package models;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectDatabase {

    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/learn_java_jdbc", "root", "123456");
        } catch (Exception e) {
            connection = null;
        }
        return connection;
    }

}
package demo;

import models.ProductModel;

public class Main {

    public static void main(String[] args) {

        ProductModel productModel = new ProductModel();
        System.out.println("Sum Quantities: " + productModel.sumQuantities());
        System.out.println("Total: " + productModel.total());

    }

}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>LearnJavaJDBC</groupId>
    <artifactId>LearnJavaJDBC</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description>Learn Java JDBC with Real Apps</description>
    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>

    </dependencies>
</project>

Screenshots