06. JSP와 DB 연동하기 2

06. JSP와 DB 연동하기 2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

package com.product.model; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class ProductDAO { Connection con = null ; PreparedStatement pstmt = null ; ResultSet rs = null ; String sql = null ; //싱글톤 방식으로 ProductDAO 만들기 //1단계: 싱글톤 방식으로 객체를 만들기 위해서는 기본 생성자의 접근 제어자를 private으로 해주어야 함. //2단계: ProductDAO 객체를 static 멤버로 선언해야 함. private static ProductDAO instance = null ; private ProductDAO() { } //3단계: 기본 생성자 대신에 싱글턴 객체를 return 해주는 getInstance() public static ProductDAO getinstance() { if (instance = = null ) { instance = new ProductDAO(); } return instance; } //DB를 연동 public void openConn() { String driver = "oracle.jdbc.driver.OracleDriver" ; String url = "jdbc:oracle:thin:@localhost:1521:xe" ; String user = "web" ; String password = "1234" ; try { Class.forName(driver); con = DriverManager.getConnection(url, user, password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public List < ProductDTO > getProductList() { List < ProductDTO > list = new ArrayList < ProductDTO > (); //오라클 DB 연동 openConn(); try { sql = "select * from products order by pnum desc" ; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { ProductDTO dto = new ProductDTO(); dto.setPnum(rs.getInt( "pnum" )); dto.setCategory_fk(rs.getString( "category_fk" )); dto.setProducts_name(rs.getString( "products_name" )); dto.setEp_code_fk(rs.getNString( "ep_code_fk" )); dto.setInput_price(rs.getInt( "input_price" )); dto.setOutput_price(rs.getInt( "output_price" )); dto.setTrans_cost(rs.getInt( "trans_cost" )); dto.setMileage(rs.getInt( "mileage" )); dto.setCompany(rs.getString( "company" )); list. add (dto); } rs.close(); pstmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } //category 테이블에서 카테고리 전체 리스트를 조회 public List < CategoryDTO > getCategoryList() { List < CategoryDTO > list = new ArrayList < CategoryDTO > (); openConn(); try { sql = "select * from category order by category_code" ; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { CategoryDTO dto = new CategoryDTO(); dto.setCnum(rs.getInt( "cnum" )); dto.setCategory_code(rs.getString( "category_code" )); dto.setCategory_name(rs.getString( "category_name" )); list. add (dto); } rs.close(); pstmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } public int insertProduct(ProductDTO dto) { int result = 0 ; int count = 0 ; openConn(); try { sql = "select max(pnum) from products" ; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); if (rs.next()) { count = rs.getInt( 1 ) + 1 ; } sql = "insert into products values(?, ?, ?, ?, ?, ?, ?, ?, ?, 1)" ; pstmt = con.prepareStatement(sql); pstmt.setInt( 1 , count); pstmt.setString( 2 , dto.getCategory_fk()); pstmt.setString( 3 , dto.getProducts_name()); pstmt.setString( 4 , dto.getEp_code_fk()); pstmt.setInt( 5 , dto.getInput_price()); pstmt.setInt( 6 , dto.getOutput_price()); pstmt.setInt( 7 , dto.getTrans_cost()); pstmt.setInt( 8 , dto.getMileage()); pstmt.setString( 9 , dto.getCompany()); result = pstmt.executeUpdate(); rs.close(); pstmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public ProductDTO getContentProduct( int num) { ProductDTO dto = new ProductDTO(); openConn(); try { sql = "select * from products where pnum = ?" ; pstmt = con.prepareStatement(sql); pstmt.setInt( 1 , num); rs = pstmt.executeQuery(); while (rs.next()) { dto.setPnum(rs.getInt( "pnum" )); dto.setCategory_fk(rs.getString( "category_fk" )); dto.setProducts_name(rs.getString( "products_name" )); dto.setEp_code_fk(rs.getNString( "ep_code_fk" )); dto.setInput_price(rs.getInt( "input_price" )); dto.setOutput_price(rs.getInt( "output_price" )); dto.setTrans_cost(rs.getInt( "trans_cost" )); dto.setMileage(rs.getInt( "mileage" )); dto.setCompany(rs.getString( "company" )); } rs.close(); pstmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return dto; } public int updateProduct(ProductDTO dto) { int result = 0 ; openConn(); try { sql = "update products set input_price=?, output_price=?, " + "trans_cost=?, mileage=?, company=? where pnum = ?" ; pstmt = con.prepareStatement(sql); pstmt.setInt( 6 , dto.getPnum()); pstmt.setInt( 1 , dto.getInput_price()); pstmt.setInt( 2 , dto.getOutput_price()); pstmt.setInt( 3 , dto.getTrans_cost()); pstmt.setInt( 4 , dto.getMileage()); pstmt.setString( 5 , dto.getCompany()); result = pstmt.executeUpdate(); pstmt.close(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public int deleteProduct( int num) { int result = 0 ; openConn(); try { sql = "delete from products where pnum=?" ; pstmt = con.prepareStatement(sql); pstmt.setInt( 1 , num); result = pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public void updateCount( int num) { openConn(); try { sql = "update products set pnum = pnum-1 where pnum > ?" ; pstmt = con.prepareStatement(sql); pstmt.setInt( 1 , num); pstmt.executeUpdate(); pstmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } Colored by Color Scripter

from http://purewater-practice-diary.tistory.com/49 by ccl(A) rewrite - 2021-10-22 19:27:42