Call Stored Procedure With No Parameters in Java JDBC

This project will demonstrate how to Call Stored Procedure With No Parameters 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 entities.Product;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ProductModel {

    public List<Product> findAll() {
        List<Product> products = new ArrayList<Product>();
        try {
            CallableStatement callableStatement = ConnectDatabase.getConnection().prepareCall("{call findAll()}");
            ResultSet resultSet = callableStatement.executeQuery();
            while (resultSet.next()) {
                Product product = new Product();
                product.setId(resultSet.getInt("id"));
                product.setName(resultSet.getString("name"));
                product.setPrice(resultSet.getBigDecimal("price"));
                product.setQuantity(resultSet.getInt("price"));
                product.setDescription(resultSet.getString("description"));
                product.setDateCreated(resultSet.getDate("dateCreated"));
                product.setFeatured(resultSet.getBoolean("featured"));
                products.add(product);
            }
        } catch (Exception e) {
            products = null;
        }
        return products;
    }

}
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 java.math.BigDecimal;
import java.text.SimpleDateFormat;
import entities.Product;
import models.ProductModel;

public class Main {

    public static void main(String[] args) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");
        ProductModel productModel = new ProductModel();

        System.out.println("Call findAll Method");
        for (Product product : productModel.findAll()) {
            System.out.println("Id: " + product.getId());
            System.out.println("Name: " + product.getName());
            System.out.println("Price: " + product.getPrice());
            System.out.println("Quantity: " + product.getQuantity());
            System.out.println("Description: " + product.getDescription());
            System.out.println("Date Created: " + simpleDateFormat.format(product.getDateCreated()));
            System.out.println("Featured: " + product.isFeatured());
            System.out.println("==========================================");
        }


    }

}
<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