Skip to content

Instantly share code, notes, and snippets.

@LebedevVA2018
Created January 28, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LebedevVA2018/147e47961d5f1ead931e3bc265b51901 to your computer and use it in GitHub Desktop.
Save LebedevVA2018/147e47961d5f1ead931e3bc265b51901 to your computer and use it in GitHub Desktop.
package orderDAO.models;
import orderDAO.util.Id;
import javax.persistence.*;
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String surname;
private int phone;
private String e_mail;
public Client(int id, String name, String surname, int phone, String e_mail) {
this.id = id;
this.name = name;
this.surname = surname;
this.phone = phone;
this.e_mail = e_mail;
}
public Client() {
}
public long 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 String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public long getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
@Override
public String toString() {
return "Client{" +
"id=" + id +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
", phone=" + phone +
", e_mail='" + e_mail + '\'' +
'}' + "\n";
}
}
package orderDAO.dao;
import orderDAO.models.Client;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public interface ClientDAO {
//create
void add(Scanner scanner) throws SQLException;
//read
List<Client> getAll() throws SQLException;
}
package orderDAO.services;
import orderDAO.dao.ClientDAO;
import orderDAO.models.Client;
import orderDAO.util.Util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ClientDAOImpl extends Util implements ClientDAO {
Connection connection = getConnection();
@Override
public void add(Scanner scanner) throws SQLException {
String sql =
"INSERT INTO CLIENTS (name, surname, phone, e_mail) VALUES (?, ?, ?, ?)";
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter surname: ");
String surname = scanner.nextLine();
System.out.print("Enter phone: ");
String sPhone = scanner.nextLine();
int phone = Integer.parseInt(sPhone);
System.out.print("Enter e_mail: ");
String e_mail = scanner.nextLine();
PreparedStatement ps = connection.prepareStatement(sql);
try {
ps.setString(1, name);
ps.setString(2, surname);
ps.setInt(3, phone);
ps.setString(4, e_mail);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps.close();
}
}
@Override
public List<Client> getAll() throws SQLException {
List<Client> clientList = new ArrayList<>();
String sql = "SELECT * FROM CLIENTS";
PreparedStatement ps = connection.prepareStatement(sql);
try {
ResultSet rs = ps.executeQuery();
try {
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String surname = rs.getString(3);
int phone = rs.getInt(4);
String e_mail = rs.getString(5);
clientList.add(new Client(id, name, surname, phone, e_mail));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs.close();
}
} finally {
ps.close();
}
return clientList;
}
}
package orderDAO.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Id {
}
package orderDAO.models;
import orderDAO.util.Id;
import javax.persistence.*;
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private int id_client;
private int id_product;
private int count;
public Order(int id, int id_client,int id_product, int count) {
this.id = id;
this.id_client = id_client;
this.id_product = id_product;
this.count = count;
}
public Order() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getId_product() {
return id_product;
}
public void setId_product(int id_product) {
this.id_product = id_product;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getId_client() {
return id_client;
}
public void setId_client(int id_client) {
this.id_client = id_client;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", id_client=" + id_client +
", id_product=" + id_product +
", count=" + count +
'}' + "\n";
}
}
package orderDAO.dao;
import orderDAO.models.Order;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public interface OrderDAO {
//create
void add(Scanner scanner) throws SQLException;
//read
List<Order> getAll() throws SQLException;
}
package orderDAO.services;
import orderDAO.dao.OrderDAO;
import orderDAO.models.Order;
import orderDAO.util.Util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class OrderDAOImpl extends Util implements OrderDAO {
Connection connection = getConnection();
@Override
public void add(Scanner scanner) throws SQLException {
String sql =
"INSERT INTO ORDERS (id_client,id_product,count) VALUES (?, ?, ?)";
System.out.print("Enter id client: ");
String sId_client = scanner.nextLine();
int id_client = Integer.parseInt(sId_client);
System.out.print("Enter id product: ");
String sId_product = scanner.nextLine();
int id_product = Integer.parseInt(sId_product);
System.out.print("Enter the quantity of goods: ");
String sCount = scanner.nextLine();
int count = Integer.parseInt(sCount);
PreparedStatement ps = connection.prepareStatement(sql);
try {
ps.setInt(1, id_client);
ps.setInt(2, id_product);
ps.setInt(3, count);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps.close();
}
}
@Override
public List<Order> getAll() throws SQLException {
List<Order> orderList = new ArrayList<>();
String sql = "SELECT * FROM ORDERS";
PreparedStatement ps = connection.prepareStatement(sql);
try {
ResultSet rs = ps.executeQuery();
try {
while (rs.next()) {
int id = rs.getInt(1);
int id_client = rs.getInt(2);
int id_product = rs.getInt(3);
int count = rs.getInt(4);
orderList.add(new Order(id, id_client, id_product, count));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs.close();
}
} finally {
ps.close();
}
return orderList;
}
}
package orderDAO.models;
import orderDAO.util.Id;
import javax.persistence.*;
public class Product {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private int id;
private String name;
private int price;
public Product(int id, String name, int price) {
this.id = id;
this.name = name;
this.price = price;
}
public Product() {
}
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}' + "\n";
}
}
package orderDAO.dao;
import orderDAO.models.Product;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public interface ProductDAO {
//create
void add(Scanner scanner) throws SQLException;
//read
List<Product> getAll() throws SQLException;
}
package orderDAO.services;
import orderDAO.dao.ProductDAO;
import orderDAO.models.Product;
import orderDAO.util.Util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ProductDAOImpl extends Util implements ProductDAO {
Connection connection = getConnection();
@Override
public void add(Scanner scanner) throws SQLException {
String sql =
"INSERT INTO PRODUCTS (name, price) VALUES (?, ?)";
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter price: ");
String sPrice = scanner.nextLine();
int price = Integer.parseInt(sPrice);
PreparedStatement ps = connection.prepareStatement(sql);
try {
ps.setString(1, name);
ps.setInt(2, price);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps.close();
}
}
@Override
public List<Product> getAll() throws SQLException {
List<Product> productList = new ArrayList<>();
String sql = "SELECT * FROM PRODUCTS";
PreparedStatement ps = connection.prepareStatement(sql);
try {
ResultSet rs = ps.executeQuery();
try {
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
int price = rs.getInt(3);
productList.add(new Product(id, name, price));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs.close();
}
} finally {
ps.close();
}
return productList;
}
}
package orderDAO;
import orderDAO.services.ClientDAOImpl;
import orderDAO.services.OrderDAOImpl;
import orderDAO.services.ProductDAOImpl;
import orderDAO.util.Util;
import java.sql.SQLException;
import java.util.Scanner;
public class Runner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ClientDAOImpl clientDAO = new ClientDAOImpl();
ProductDAOImpl productDAO = new ProductDAOImpl();
OrderDAOImpl orderDAO = new OrderDAOImpl();
try {
try {
// create connection
Util util = new Util();
util.getConnection();
while (true) {
System.out.println("1: add client");
System.out.println("2: show all clients");
System.out.println("3: add product");
System.out.println("4: show all products");
System.out.println("5: create order");
System.out.println("6: display a list of orders");
System.out.print("-> ");
String s = sc.nextLine();
switch (s) {
case "1":
clientDAO.add(sc);
break;
case "2":
System.out.println(clientDAO.getAll());
break;
case "3":
productDAO.add(sc);
break;
case "4":
System.out.println(productDAO.getAll());
break;
case "5":
orderDAO.add(sc);
break;
case "6":
System.out.println(orderDAO.getAll());
break;
default:
return;
}
}
} finally {
sc.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
return;
}
}
}
package orderDAO.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Util {
private static final String DB_DRIVER = "org.h2.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/orders?serverTimezone=Europe/Kiev";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "karmen1404";
public Connection getConnection() {
Connection connection = null;
try {
Class.forName(DB_DRIVER);
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
System.out.println("Successful connection");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
System.out.println("Connection error");
}
return connection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment