Create Entity in JDBC


Copy JAR files which are listed below:

mysql-connector-java-5.1.36.jar

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');




Create new package named entities. In this package, create new java class named Product.java as below:

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

}




In models package, create new java class named ProductModel.java contains methods to interact with the database.

package models;

import entities.Product;
import java.sql.PreparedStatement;

public class ProductModel {

	public boolean create(Product product) {
		boolean result = true;
		try {
			PreparedStatement preparedStatement = ConnectDatabase.getConnection().prepareStatement("insert into product(name, price, quantity, description, featured, dateCreated) values(?, ?, ?, ?, ?, ?)");
			preparedStatement.setString(1, product.getName());
			preparedStatement.setBigDecimal(2, product.getPrice());
			preparedStatement.setInt(3, product.getQuantity());
			preparedStatement.setString(4, product.getDescription());
			preparedStatement.setBoolean(5, product.isFeatured());
			preparedStatement.setDate(6, new java.sql.Date(product.getDateCreated().getTime()));
			result = preparedStatement.executeUpdate() > 0;
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

}




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.util.Date;
import entities.Product;
import models.ProductModel;

public class Main {

	public static void main(String[] args) {

		ProductModel productModel = new ProductModel();
		Product product = new Product();
		product.setName("Product 4");
		product.setPrice(BigDecimal.valueOf(10));
		product.setQuantity(6);
		product.setDescription("Good");
		product.setFeatured(true);
		product.setDateCreated(new Date());
		boolean result = productModel.create(product);
		System.out.println("Result: " + result);

	}

}
Result: true