Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Программирование на Java часть 2

.pdf
Скачиваний:
134
Добавлен:
16.03.2016
Размер:
3.07 Mб
Скачать

}

public void setBlock_status(int block_status) { this.block_status = block_status;

}

public int getDelete_status() { return delete_status;

}

public void setDelete_status(int delete_status) { this.delete_status = delete_status;

}

}

package by.belhard.entity;

public class Goods {

private int goods_id; private String name; private String imagePath; private int price;

private String description; private int delete_status;

public Goods(int goods_id) { this.goods_id = goods_id; this.name = ""; this.imagePath = ""; this.price = 0; this.description = ""; this.delete_status = 0;

}

public Goods(int goods_id, String name, String imagePath, int price, String description, int delete_status) {

this.goods_id = goods_id; this.name = name; this.imagePath = imagePath; this.price = price; this.description = description; this.delete_status = delete_status;

}

121

public Goods(int goods_id, String imagePath) { this.goods_id = goods_id;

this.imagePath = imagePath;

}

public Goods(int goods_id, String name, int price, String description) {

this.goods_id = goods_id; this.name = name; this.price = price;

this.description = description;

}

public int getGoods_id() { return goods_id;

}

public String getDescription() { return description;

}

public void setGoods_id(int goods_id) { this.goods_id = goods_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;

}

public int getDelete_status() { return delete_status;

}

122

public void setDelete_status(int delete_status) { this.delete_status = delete_status;

}

public String getImagePath() { return imagePath;

}

@Override

public String toString() {

return "Goods{" + "goods_id=" + goods_id + ", name=" + name + ","

+" imagePath=" + imagePath + ", price=" + price

+", description=" + description + ", delete_status="

+delete_status + '}';

}

}

package by.belhard.entity;

public class Orders {

private int orders_id; private int users_id; private String payment; private int delete_status; private int total_cost;

public Orders(int orders_id, int users_id, String payment, int delete_status, int total_cost) {

this.orders_id = orders_id; this.users_id = users_id; this.payment = payment; this.delete_status = delete_status; this.total_cost = total_cost;

}

public Orders(int users_id, String payment, int delete_status, int total_cost) {

this.users_id = users_id; this.payment = payment; this.delete_status = delete_status; this.total_cost = total_cost;

}

123

public Orders(int orders_id) { this.orders_id = orders_id; this.users_id = 0; this.payment = "processing"; this.delete_status = 0;

}

public Orders(int orders_id, String payment) { this.orders_id = orders_id;

this.payment = payment;

}

public int getOrders_id() { return orders_id;

}

public void setOrders_id(int orders_id) { this.orders_id = orders_id;

}

public int getUsers_id() { return users_id;

}

public void setUsers_id(int users_id) { this.users_id = users_id;

}

public String getPayment() { return payment;

}

public void setPayment(String payment) { this.payment = payment;

}

public int getDelete_status() { return delete_status;

}

public void setDelete_status(int delete_status) { this.delete_status = delete_status;

}

124

public int getTotal_cost() { return total_cost;

}

public void setTotal_cost(int total_cost) { this.total_cost = total_cost;

}

}

package by.belhard.entity;

public class Goods_in_orders { private int orders_id; private int goods_id;

private int count;

public Goods_in_orders(int orders_id, int goods_id, int count) { this.orders_id = orders_id;

this.goods_id = goods_id; this.count = count;

}

public Goods_in_orders(int orders_id) { this.orders_id = orders_id;

}

public int getCount() { return count;

}

public void setCount(int count) { this.count = count;

}

public int getGoods_id() { return goods_id;

}

public void setGoods_id(int goods_id) { this.goods_id = goods_id;

}

public int getOrders_id() { return orders_id;

}

125

public void setOrders_id(int orders_id) { this.orders_id = orders_id;

}

}

Тема 4.4. Слой DAO

В пакете dao создадим параметризированный DAOInterface, в котором объявим основные методы при работе с любой таблицей.

insert –для добавления новой строки в талицу; delete – для изменения delete_status;

update – для редактирования всех столбцов по первичному ключу; softDelete – для удаления строки из таблицы.

Листинг 4.3

package by.belhard.dao; import java.sql.SQLException;

public interface DAOInterface<T> {

public void insert(T ob)throws SQLException; public void delete(T ob)throws SQLException; public void update(T ob)throws SQLException; public void softDelete(T ob)throws SQLException;

}

Данный интерфейс необходимо реализовать для каждого класса сущности.

Листинг 4.4

package by.belhard.dao;

import by.belhard.entity.Users; import by.belhard.mysql.DB;

import com.mysql.jdbc.PreparedStatement; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; import java.sql.ResultSet;

import java.sql.SQLException;

public class DaoUsers implements DAOInterface<Users> {

private DB db;

126

public DaoUsers(DB db) { this.db = db;

}

@Override

public void insert(Users ob) throws SQLException { PreparedStatement ps = (PreparedStatement)

db.getCn().prepareStatement(

"insert into " + ob.getClass().getSimpleName()

+" (login,pass,role,block_status,delete_status,balance)"

+" values(?,?,?,?,?,?)");

ps.setString(1, ob.getLogin()); ps.setString(2, ob.getPass()); ps.setInt(3, ob.getRole()); ps.setInt(4, ob.getBlock_status()); ps.setInt(5, ob.getDelete_status()); ps.setInt(6, ob.getBalance()); ps.execute();

}

@Override

public void update(Users ob) throws SQLException { PreparedStatement ps = (PreparedStatement)

db.getCn().prepareStatement(

"update " + ob.getClass().getSimpleName()

+" set login=?, pass=?, role=? , block_status=?,"

+" balance =? where users_id=" + ob.getUsers_id()); ps.setString(1, ob.getLogin());

ps.setString(2, ob.getPass()); ps.setInt(3, ob.getRole()); ps.setInt(4, ob.getBlock_status()); ps.setInt(5, ob.getBalance()); ps.execute();

}

public void updateBalance(Users ob) throws SQLException { int balance = 0;

ResultSet rs = db.query("select balance from users " + "where login='" + ob.getLogin() + "'");

if (rs.next()) {

balance = rs.getInt(1);

}

127

PreparedStatement ps = (PreparedStatement) db.getCn().prepareStatement(

"update " + ob.getClass().getSimpleName()

+" set balance =? where login='"

+ob.getLogin() + "'"); ps.setInt(1, balance + ob.getBalance()); ps.execute();

}

@Override

public void softDelete(Users ob) throws SQLException { db.update("delete from " + ob.getClass().getSimpleName()

+ " where users_id=" + ob.getUsers_id());

}

@Override

public void delete(Users ob) throws SQLException { db.update("update " + ob.getClass().getSimpleName()

+ " set delete_status=1 where users_id=" + ob.getUsers_id());

}

public Users getUser(ResultSet rs)

throws SQLException, NoSuchAlgorithmException, UnsupportedEncodingException {

return new Users(rs.getInt("users_id"), rs.getString("login"), rs.getString("pass"), rs.getInt("role"), rs.getInt("block_status"), rs.getInt("balance"));

}

}

package by.belhard.dao;

import by.belhard.entity.Goods; import by.belhard.mysql.DB;

import com.mysql.jdbc.PreparedStatement; import java.sql.ResultSet;

import java.sql.SQLException; import java.util.ArrayList;

public class DaoGoods implements DAOInterface<Goods> {

private DB db;

public DaoGoods(DB db) {

128

this.db = db;

}

@Override

public void insert(Goods ob) throws SQLException { PreparedStatement ps = (PreparedStatement)

db.getCn().prepareStatement(

"insert into " + ob.getClass().getSimpleName()

+"(name,image_path,price,description,delete_status)"

+" values(?,?,?,?,?)");

ps.setString(1, ob.getName()); ps.setString(2, ob.getImagePath()); ps.setInt(3, ob.getPrice()); ps.setString(4, ob.getDescription()); ps.setInt(5, ob.getDelete_status()); ps.execute();

}

@Override

public void update(Goods ob) throws SQLException { PreparedStatement ps = (PreparedStatement)

db.getCn().prepareStatement(

"update " + ob.getClass().getSimpleName()

+" set name=?, price=?,description = ? "

+"where goods_id=" + ob.getGoods_id()); ps.setString(1, ob.getName());

ps.setInt(2, ob.getPrice()); ps.setString(3, ob.getDescription()); ps.execute();

}

@Override

public void softDelete(Goods ob) throws SQLException { db.update("delete from " + ob.getClass().getSimpleName()

+ " where goods_id=" + ob.getGoods_id());

}

@Override

public void delete(Goods ob) throws SQLException { db.update("update " + ob.getClass().getSimpleName()

+ " set delete_status=1 where goods_id=" + ob.getGoods_id());

}

public ArrayList<Goods> selectAll() throws SQLException {

129

ArrayList<Goods> tmp = new ArrayList<Goods>(); ResultSet rs = this.db.query("SELECT * FROM goods"); while (rs.next()) {

tmp.add(new Goods(rs.getInt("goods_id"), rs.getString("name"), rs.getString("image_path"), rs.getInt("price"), rs.getString("description"),

rs.getInt("delete_status")));

}

return tmp;

}

public void setImagePath(Goods ob) throws SQLException { System.out.println(ob.getImagePath());

db.update("update " + ob.getClass().getSimpleName()

+" set image_path='" + ob.getImagePath().replace("\\", "\\/")

+"' where goods_id=" + ob.getGoods_id());

}

}

package by.belhard.dao;

import by.belhard.entity.Orders; import by.belhard.mysql.DB;

import com.mysql.jdbc.PreparedStatement; import java.sql.ResultSet;

import java.sql.SQLException;

public class DaoOrders implements DAOInterface<Orders> {

private DB db;

public DaoOrders(DB db) { this.db = db;

}

@Override

public void insert(Orders ob) throws SQLException {

PreparedStatement ps = (PreparedStatement) db.getCn().prepareStatement(

"insert into " + ob.getClass().getSimpleName()

+"(users_id,payment,delete_status,total_cost) "

+"values(?,?,?,?)");

ps.setInt(1, ob.getUsers_id()); ps.setString(2, ob.getPayment()); ps.setInt(3, ob.getDelete_status());

130