dao/CategoryDAO.java > MODEL2

본문 바로가기

사이트 내 전체검색

뒤로가기 MODEL2

dao/CategoryDAO.java

페이지 정보

작성자 관리자 작성일 23-03-19 10:59 조회 546 댓글 0

본문

package dao;


import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.ArrayList;


import model.Category;

import model.Member;


public class CategoryDAO {


Statement stmt;

PreparedStatement pstmt;

ResultSet rs;


DAO d = new DAO();


//총 수

public int getAllcount(String field, String search) {

d.getCon();


int count = 0;


try {

String sql = "";


if(search != null && !search.equals("")) {

sql = "select count(*) from category where "+field+" like '%"+search+"%'";

}else {

sql = "select count(*) from category";

}


stmt = d.conn.createStatement();


rs = stmt.executeQuery(sql);


if(rs.next()) {

count = rs.getInt(1);

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

stmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return count;

}


//목록

public ArrayList<Category> getAllMember(int startRow, int endRow, String field, String search) {

d.getCon();


ArrayList<Category> v = new ArrayList<Category>();


try {

String sql = "";


if(search != null && !search.equals("")) {

sql = "select * from category where "+field+" like '%"+search+"%' order by ca_id asc limit ?,?";

}else {

sql = "select * from category order by ca_id asc limit ?,?";

}


pstmt = d.conn.prepareStatement(sql);


pstmt.setInt(1, startRow);

pstmt.setInt(2, endRow);


rs = pstmt.executeQuery();


while(rs.next()) {

Category c = new Category();


c.setCa_id(rs.getString("ca_id"));

c.setCa_name(rs.getString("ca_name"));

c.setCa_use(rs.getString("ca_use"));


v.add(c);

}

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

rs.close();

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

return v;

}


//등록

public void insertCategory(Category c) {

d.getCon();


try {

String sql = "insert into category values (?,?,?)";

pstmt = d.conn.prepareStatement(sql);


pstmt.setString(1, c.getCa_id());

pstmt.setString(2, c.getCa_name());

pstmt.setString(3, c.getCa_use());


pstmt.executeUpdate();

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}


//상세 생략

//수정

public void updateCategory(Category c) {

d.getCon();


try {

String sql = "update category set ca_name=?,ca_use=? where ca_id=?";

pstmt = d.conn.prepareStatement(sql);


pstmt.setString(1, c.getCa_name());

pstmt.setString(2, c.getCa_use());

pstmt.setString(3, c.getCa_id());


pstmt.executeUpdate();

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}


//삭제

public void oneDelete(String ca_id) {

d.getCon();


try {

String sql = "delete from category where ca_id=?";

pstmt = d.conn.prepareStatement(sql);


pstmt.setString(1, ca_id);


pstmt.executeUpdate();

}catch(Exception e) {

e.printStackTrace();

}finally {

try {

pstmt.close();

d.conn.close();

}catch(Exception e) {

e.printStackTrace();

}

}

}

}


추천0

댓글목록 0

등록된 댓글이 없습니다.


Copyright © dancePKT. All rights reserved.
PC 버전으로 보기