IT 관련,,/Java관련 언어들

[웹/JSP]jsp model1 간단한 도서관리게시판-4(update문)

IT깡패's 2020. 8. 9. 11:21
728x90
반응형
SMALL

이번 시간에 update문에 대해서 이야기해보겠습니다~ㅎㅎ

update문은 updateForm.jsp, updateProc.jsp, BeanDao(update문) 이렇게 구성되어 있는데요~

insert문에서 insert할 때, 서점컬럼이 들어올 때, 띄어쓰기를 해서 값을 주었는데, update에서는

쉼표를 해서 값에 들어가게끔 해줘보았습니다~!!

자 그럼 update문의 코드 분석을 한번 해보도록 하겠습니다~ㅎㅎ

 

- updateForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="book.BookBean" %>    

<jsp:useBean id="dao" class="book.BookDao" />

<%
	request.setCharacterEncoding("UTF-8");    
    String number = request.getParameter("bnum");
    if (number == null) {
    	number = "0";
		}
	int bnum = Integer.parseInt(number);	
	
	BookBean bb = dao.getUpdateByBnum(bnum); 
%>
	<h2>영화 정보 수정</h2>
	<form action="updateProc.jsp" method="post" name="myform">
	<input type="hidden" name="bnum" value="<%=bnum%>"><br>
	<table border="1">
	<tr>
		<td>제목</td>
		<td> <input type="text" name="title" value="<%=bb.getTitle() %>"></td>
	</tr>
	<tr>	
		<td>저자</td>
		<td> <input type="text" name="author" value="<%=bb.getAuthor() %>"></td>
	</tr>
		
	<tr>
		<td>출판사</td><td><input type="text" name="publisher" value="<%=bb.getPublisher() %>"></td>
	</tr>
	<tr>
				<td>가격</td>
				<td><input type="text" name="price" value="<%=bb.getPrice() %>"></td>
			</tr>
			<tr>
				<td>입고일</td>
				<td><input type="date" name="bdate" value="<%=bb.getBdate() %>"></td>
			</tr>

			<tr>
				<td>배송비</td>
				<td>유료 <input type="radio" name="kind" value="유료" <%if(bb.getKind().contains("유료")) {%> checked <%} %>>
					무료<input type="radio" name="kind" value="무료" <%if(bb.getKind().contains("무료")) {%> checked <%} %>>
														
				</td>	
			</tr>
			<tr>
				<td>구입가능 서점</td>
				<td>교보문고 <input type="checkbox" name="bookstore" value="kyobo" <%if(bb.getBookstore().contains("kyobo")) { %> checked <%} %>>
					알라딘 <input type="checkbox" name="bookstore" value="aladin" <%if(bb.getBookstore().contains("aladin")) { %> checked <%} %>>
					yes24 <input type="checkbox" name="bookstore" value="yes24" <%if(bb.getBookstore().contains("yes24")) { %> checked <%} %>>
					인터파크 <input type="checkbox" name="bookstore" value="interpark" <%if(bb.getBookstore().contains("interpark")) { %> checked <%} %>>
					</td>
			</tr>
			<tr>
				<td>보유수량</td>
				<td><select name="count">
						<option value="선택">선택</option>
						<option value="1" <% if((bb.getCount()).equals("1")) {%> selected <% } %>> 1</option>
						<option value="2" <% if((bb.getCount()).equals("2")) {%> selected <% } %>> 2</option>
						<option value="3" <% if((bb.getCount()).equals("3")) {%> selected <% } %>> 3</option>
						<option value="4" <% if((bb.getCount()).equals("4")) {%> selected <% } %>> 4</option>
						<option value="5" <% if((bb.getCount()).equals("5")) {%> selected <% } %>> 5</option>
						
						
				</select></td>
			</tr>

		<tr align="center">
			<td colspan="2"><input type="submit" value="수정하기"></td>
		</tr>
		</table>
	
	</form> 
</body>
</html>

 

저기서 하는 방식은 스크립트릿을 이용해 input창의 value값을 고정시키도 업데이트 시키게끔 해주기위한 방식이라고

생각하시면 되겠고요~!

가끔 int bnum만 하면 업데이트 부분에서 null값(nullpointException)이 떠서 저렇게 number=null값이라고 처리해

주었습니다~

자 이게 이쪽 form(폼) 액션을 위한 updateProc.jsp창을 만들어보겠습니다요~!!

 

- updateProc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%	request.setCharacterEncoding("UTF-8");%>
<jsp:useBean id="dao" class="book.BookDao"/>
<jsp:useBean id="bean" class="book.BookBean"/>
<jsp:setProperty name="bean" property="*" />


<%
	
	String[] bookstore = request.getParameterValues("bookstore");
	
	String temp = " "; 
	if (bookstore == null){
		temp="선택사항없음"; 
					
	}			
	else {
		for (int i = 0; i < bookstore.length; i++) {
			if(i == bookstore.length-1)
				temp += bookstore[i] ;
			else
				temp += bookstore[i] + ","  ;
		}
	}
	bean.setBookstore(temp); 
		
	int cnt=-1;
	
	cnt = dao.updateData(bean);
	if(cnt>0)
		response.sendRedirect("select.jsp");
	else
		response.sendRedirect("updateForm.jsp");
%>

 

자 저렇게 해서 setter처리로 bookstore가 위에서 말한것과 같이 쉼표(,)로 나오도록 해주었고요~

cnt값이 아무값도 없으면 updateForm.jsp로 머무르게끔하고 값이 들어가지면 select.jsp로 이동하게되지요~

그리고 업데이트 dao값도 올려보겠습니다~

 

- BookDao.java

package book;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import book.BookBean;

public class BookDao {
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:xe";
	String dbid="scott";
	String dbpw="1234";
	Connection conn = null;

	public BookDao() {

		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,dbid,dbpw);
			
		}catch(SQLException e){
			e.printStackTrace();

		}catch(ClassNotFoundException e){
			e.printStackTrace();

		}catch(Exception e){
			e.printStackTrace();
		}
	}


	public Vector<BookBean> getAllBook(){
		String sql = "select * from book order by bnum";
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		Vector<BookBean> lists = new Vector<BookBean>();
		try{
			pstmt=conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				int bnum = rs.getInt("bnum");
				String title = rs.getString("title");
				String author = rs.getString("author");
				String publisher = rs.getString("publisher");
				String price = rs.getString("price");
				String bdate = rs.getString("bdate");
				String kind = rs.getString("kind");
				String bookstore = rs.getString("bookstore");
				String count = rs.getString("count"); 
				BookBean bb = new BookBean(bnum,title,author,publisher,price,bdate,kind,bookstore,count);
				lists.add(bb);
			}
			

		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(rs!=null)
					rs.close();
				if(pstmt!=null)
					pstmt.close();
				if(conn!=null)
					conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return lists; 
	}

	public int insertData(BookBean bean) {
		PreparedStatement pstmt = null;
		String sql = "insert into book(bnum,title,author,publisher,price,bdate,kind,bookstore,count) "
				+ "values(b_seq.nextval,?,?,?,?,?,?,?,?)";
		int cnt=-1;
		try{
			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, bean.getTitle());
			pstmt.setString(2, bean.getAuthor());
			pstmt.setString(3, bean.getPublisher());
			pstmt.setString(4, bean.getPrice());
			pstmt.setString(5, bean.getBdate());
			pstmt.setString(6,bean.getKind());
			pstmt.setString(7, bean.getBookstore());
			pstmt.setString(8, bean.getCount());

			cnt=pstmt.executeUpdate();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(pstmt!=null)
					pstmt.close();
				if(conn!=null)
					conn.close();
			}catch(Exception e2){
				e2.printStackTrace();
			}
		}
		return cnt;			
	}

	public BookBean getUpdateByBnum(int bnum){ 

		String sql = "select * from book where bnum=?";
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		BookBean bean=null;
		try{
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1,bnum);
			rs = pstmt.executeQuery();

			if(rs.next()){ 
				String title = rs.getString("title");
				String author = rs.getString("author");
				String publisher = rs.getString("publisher");
				String price = rs.getString("price");
				String bdate = rs.getString("bdate");
				String kind = rs.getString("kind");
				String bookstore = rs.getString("bookstore");
				String count = rs.getString("count"); 
				bean = new BookBean(0,title,author,publisher,price,bdate,kind,bookstore,count);				
			}
				
		} catch (Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(rs!=null)
					rs.close();
				if(pstmt!=null)
					pstmt.close();
				if(conn!=null)
					conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		return bean;		
	}
	
	public int updateData(BookBean bean){
		PreparedStatement pstmt=null;

		String sql = "update book set  title=?, author=?, publisher=?, price=?, bdate=?, kind=?, bookstore=?, count=? where bnum=?";
		int cnt = -1;

		try{
			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, bean.getTitle());
			pstmt.setString(2, bean.getAuthor());
			pstmt.setString(3, bean.getPublisher());
			pstmt.setString(4, bean.getPrice());
			pstmt.setString(5, bean.getBdate());
			pstmt.setString(6,bean.getKind());
			pstmt.setString(7, bean.getBookstore());
			pstmt.setString(8, bean.getCount());
			pstmt.setInt(9, bean.getBnum());
			
			cnt = pstmt.executeUpdate();
			
		}catch(Exception e){
			e.printStackTrace();

		}finally{
			try{
				if(pstmt!=null)
					pstmt.close();
				if(conn!=null)
					conn.close();
			}catch(Exception e2){
				e2.printStackTrace();
			}
		}
		return cnt;
	}
	
}

 

getgetUpdateByBnum와 updateData메서드를 넣어서 업데이트가 유지되고 바뀌게끔 해주었는데요~

어서어서 파악을 해서 update도 능통(?)하게끔 익히고 공부해주면 되겠습니다~!!

728x90
반응형
LIST