<%@page import="pack.Dbex1Dto"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:useBean id="test" class="pack.Dbex1" scope="page"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>** 사원 목록(beans + db pooling 사용)**</h2>
<table width="300" border="1">
<tr>
<th>사번</th><th>이름</th><th>부서명</th><th>직급</th><th>성별</th>
</tr>
<%
ArrayList<Dbex1Dto> list = test.getDataAll();
for(Dbex1Dto s : list){ //향상된 for문장 : 컬렉션에 있는 데이터를 쉽게 접근가능
%>
<tr>
<td><%=s.getSawon_no() %></td>
<td><a href="dbex1_1.jsp?gogek_damsano=<%=s.getSawon_no() %>" target="my"><%=s.getSawon_name() %></a></td>//GET방식으로 넘김
<td><%=s.getBuser_name() %></td>
<td><%=s.getSawon_jik() %></td>
<td><%=s.getSawon_gen() %></td>
</tr>
<%
}
%>
</table>
<hr>
<iframe name="my" width="100%" height="600"></iframe>
</body>
</html>
================================================================================================================================
<%@page import="pack.Dbex1Dto"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:useBean id="test" class="pack.Dbex1" scope="page"/>
<jsp:useBean id="dto" class="pack.Dbex1Dto" scope="page"/>
<jsp:setProperty property="gogek_damsano" name="dto"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="300" border="1">
<tr>
<th>고객번호</th><th>고객이름</th><th>고객전화</th><th>성별</th>
</tr>
<%
ArrayList<Dbex1Dto> list = test.getDataGogek(dto.getGogek_damsano()); //bean태그로 생성한 객체는 해당 bean태그의 id로 접근가능
for(Dbex1Dto s : list){
%>
<tr>
<td><%=s.getGogek_no() %></td>
<td><%=s.getGogek_name() %></td>
<td><%=s.getGogek_tel() %></td>
<td><%=s.getGogek_gen() %></td>
</tr>
<%
}
%>
</table>
<hr>
</body>
</html>
================================================================================================================================
package pack;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class Dbex1 {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private DataSource ds;
public Dbex1() {
try {
//dbcp - apach 제공'
Context context = new InitialContext();
ds = (DataSource)context.lookup("java:comp/env/jdbc/Oracle");
} catch (Exception e) {
System.out.println("db 연결 실패 : " + e);
}
}
public ArrayList<Dbex1Dto> getDataAll(){
ArrayList<Dbex1Dto> list = new ArrayList<>();
String sql = "SELECT * FROM sawon LEFT OUTER JOIN buser ON sawon.buser_num=buser.buser_no ORDER BY sawon_no";
try{
conn = ds.getConnection(); // Server의 context.xml에 설정한 값을 셋팅됨
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
Dbex1Dto dto = new Dbex1Dto();
dto.setSawon_no(rs.getString("sawon_no"));
dto.setSawon_name(rs.getString("sawon_name"));
dto.setBuser_name(rs.getString("buser_name"));
dto.setSawon_jik(rs.getString("sawon_jik"));
dto.setSawon_gen(rs.getString("sawon_gen"));
list.add(dto);
}
}catch(Exception e){
System.out.println("처리 실패 : " + e);
}finally{
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (Exception e2) {
}
}
return list;
}
public ArrayList<Dbex1Dto> getDataGogek(String damsano){
ArrayList<Dbex1Dto> list = new ArrayList<>();
String sql = "SELECT gogek_no,gogek_name,gogek_tel,gogek_jumin,CASE";
sql+= " WHEN SUBSTR(gogek.gogek_jumin,8,1) = 1 THEN '남'";
sql+= " WHEN SUBSTR(gogek.gogek_jumin,8,1) = 2 THEN '여'";
sql+= " END AS gogek_gen FROM gogek WHERE gogek_damsano=?";
try{
conn = ds.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, damsano);
rs = pstmt.executeQuery();
while(rs.next()){
Dbex1Dto dto = new Dbex1Dto();
dto.setGogek_no(rs.getString("gogek_no"));
dto.setGogek_name(rs.getString("gogek_name"));
dto.setGogek_tel(rs.getString("gogek_tel"));
// dto.setGogek_gen(rs.getString("gogek_gen"));
dto.setGogek_jumin(rs.getString("gogek_jumin"));
if(dto.getGogek_jumin().substring(8,9).equals("1")){
dto.setGogek_gen("남");
}else{
dto.setGogek_gen("여");
}
list.add(dto);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch (Exception e2) {
}
}
return list;
}
}
================================================================================================================================
DTO 생략...
'JSP(Java Server Page)' 카테고리의 다른 글
MVC1 패턴을 이용한 간단한 게시판 구현 (0) | 2014.03.28 |
---|---|
redirect와 forward 사용을 구분하는 법 (0) | 2014.03.27 |
dbcp객체를 server환경설정으로 다루기(싱글톤아님) (0) | 2014.03.21 |
beans를 이용하여 여러개의 데이터를 주고받기 (0) | 2014.03.21 |
beans를 사용하여 데이터 주고받기 (0) | 2014.03.21 |