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');
Structure of Product Table
Data of Product Table
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 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;
}
}
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 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());
}
}
Output
Sum Quantities: 38
Total: 552.0