Java Libraries
Copy JAR files which are listed below:
mysql-connector-java-5.1.36.jar
Create Database
Create a database with the name is advancedjava. This database have 1 tables: Product table.
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
`price` decimal(10,1) NOT NULL,
`quantity` int(11) NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`featured` tinyint(1) NOT NULL,
`dateCreated` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`name`, `price`, `quantity`, `description`, `featured`, `dateCreated`) VALUES
('laptop 1', '2.0', 5, 'Description of laptop 1', 1, '2018-03-19'),
('laptop 2', '12.0', 7, 'Description of laptop 2', 0, '2018-03-21'),
('computer 1', '4.0', 6, 'Description of computer 1', 1, '2018-03-23'),
('computer 2', '10.0', 6, 'Description of computer 2', 1, '2018-03-14'),
('computer 3', '21.0', 4, 'Description of computer 3', 0, '2018-03-26'),
('tivi 1', '17.0', 2, 'Description of tivi 1', 1, '2018-03-23'),
('tivi 2', '32.0', 8, 'Description of tivi 2', 0, '2018-03-29');
--
-- Procedures
--
DELIMITER $$
CREATE PROCEDURE findAll()
BEGIN
SELECT * FROM product;
END $$
DELIMITER ;
Structure of Product Table
Data of Product Table
Entities Class
Create new package named entities. In this package, create new java class named Product.java as below:
Product Entity
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;
}
}
Create ConnectDatabase Class
Create new package named models. In this package, create new java class named ConnectDatabase.java as below:
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.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/advancedjava", "root", "123456");
} catch (Exception e) {
connection = null;
}
return connection;
}
}
Create ProductModel Class
In models package, create new java class named ProductModel.java contains methods to interact with the database.
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;
}
}
Structure of Project
Run Application
Create new package named demo. In this package, create new java class named Main.java as below:
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("==========================================");
}
}
}
Output
Call findAll Method
Id: 1
Name: Name 1
Price: 2.0
Quantity: 2
Description: Description of product 1
Date Created: 03/19/2018
Featured: true
==========================================
Id: 2
Name: Name 2
Price: 5.5
Quantity: 5
Description: Description of product 2
Date Created: 03/20/2018
Featured: false
==========================================
Id: 3
Name: Name 3
Price: 12.0
Quantity: 12
Description: Description of product 3
Date Created: 03/21/2018
Featured: false
==========================================