반응형
ProductVo
package product;
public class ProductVo {
private int num;
private String name;
private int price;
private int amount;
public ProductVo() {
}
public ProductVo(int num, String name, int price, int amount) {
super();
this.num = num;
this.name = name;
this.price = price;
this.amount = amount;
}
public int getNum() {
return num;
}
public void setNumb(int numb) {
this.num = num;
}
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 getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public String toString() {
return "ProductVo [numb=" + num + ", name=" + name + ", price=" + price + ", amount=" + amount + "]";
}
}
ProductDao
package product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import conn.DBConnect;
public class ProductDao {
private DBConnect dbconn;
public ProductDao() {
dbconn = DBConnect.getInstance();
}
public void insert(ProductVo vo) {
Connection conn = dbconn.conn();
String sql = "insert into product values(seq_product.nextval, ?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, vo.getName());
pstmt.setInt(2, vo.getPrice());
pstmt.setInt(3, vo.getAmount());
int num = pstmt.executeUpdate();
System.out.println(num + "줄이 추가됨");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public ProductVo selectByNum(int num) {
Connection conn = dbconn.conn();
String sql = "select * from product where num=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, num);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public ArrayList<ProductVo> selectByName(String name) {
ArrayList<ProductVo> list = new ArrayList<ProductVo>();
Connection conn = dbconn.conn();
String sql = "select * from product where name=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public ArrayList<ProductVo> selectByPrice(int price1, int price2) {
ArrayList<ProductVo> list = new ArrayList<ProductVo>();
Connection conn = dbconn.conn();
String sql = "select * from product where price between ? and ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, price1);
pstmt.setInt(2, price2);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public ArrayList<ProductVo> selectAll() {
ArrayList<ProductVo> list = new ArrayList<ProductVo>();
Connection conn = dbconn.conn();
String sql = "select * from product";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add(new ProductVo(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getInt(4)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public void update(ProductVo vo) {
Connection conn = dbconn.conn();
String sql = "update product set price=?, amount=? where num=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, vo.getPrice());
pstmt.setInt(2, vo.getAmount());
pstmt.setInt(3, vo.getNum());
int num = pstmt.executeUpdate();
System.out.println(num + "줄이 수정됨");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void delete(int num) {
Connection conn = dbconn.conn();
String sql = "delete product where num=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, num);
int num2 = pstmt.executeUpdate();
System.out.println(num2 + "줄이 수정됨");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ProductService
package product;
import java.util.ArrayList;
public class ProductService {
private ProductDao dao;
public ProductService() {
dao = new ProductDao();
}
//제품추가
public void addProduct(ProductVo vo) {
dao.insert(vo);
}
//번호로 검색
public ProductVo getByNum(int num) {
return dao.selectByNum(num);
}
//제품명으로 검색
public ArrayList<ProductVo> getByName(String name){
return dao.selectByName(name);
}
//가격으로 검색
public ArrayList<ProductVo> getByPrice(int p1, int p2){
return dao.selectByPrice(p1, p2);
}
//전체 검색
public ArrayList<ProductVo> getAll(){
return dao.selectAll();
}
//수정
public void editProduct(ProductVo vo) {
dao.update(vo);
}
//삭제
public void delProduct(int num) {
dao.delete(num);
}
}
반응형