MemberBean.java
===============================================
package kr.co.seoulit.member.to;
public class MemberBean {
private String id, pw, addr, tel;
public void setId(String id){ this.id=id; }
public void setPw(String pw){ this.pw=pw; }
public void setAddr(String addr){ this.addr=addr; }
public void setTel(String tel){ this.tel=tel; }
public String getId(){ return id; }
public String getPw(){ return pw; }
public String getAddr(){ return addr; }
public String getTel(){ return tel; }
}
MemberDAOImpl.java
========================================================
package kr.co.seoulit.member.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import kr.co.seoulit.common.dao.DataAccessException;
import kr.co.seoulit.common.sl.ServiceLocator;
import kr.co.seoulit.member.to.MemberBean;
public class MemberDAOImpl implements MemberDAO {
private static MemberDAOImpl instance = new MemberDAOImpl();
private MemberDAOImpl() {
}
public static MemberDAOImpl getInstance() {
return instance;
}
public List<MemberBean> selectMemberList() throws DataAccessException {
List<MemberBean> v = new ArrayList<MemberBean>();
Connection con = null;
PreparedStatement pstmt = null;
try {
StringBuffer query = new StringBuffer();
query.append("select * from member");
DataSource dataSource = ServiceLocator.getInstance().getDataSource(
"jdbc/myoracle");
con = dataSource.getConnection();
pstmt = con.prepareStatement(query.toString());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
MemberBean member = new MemberBean();
member.setId(rs.getString("id"));
member.setPw(rs.getString("pw"));
member.setAddr(rs.getString("addr"));
member.setTel(rs.getString("tel"));
v.add(member);
}
return v;
} catch (Exception sqle) {
throw new DataAccessException(sqle.getMessage());
} finally {
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
public void updateMember(String id,String pw,String addr,String tel) {
Connection con = null;
PreparedStatement pstmt = null;
try {
DataSource dataSource = ServiceLocator.getInstance().getDataSource("jdbc/myoracle");
con = dataSource.getConnection();
pstmt = con.prepareStatement("update member set pw=?, addr=?, tel=? where id=?");
pstmt.setString(4, id);
pstmt.setString(1, pw);
pstmt.setString(2, addr);
pstmt.setString(3, tel);
int rs = pstmt.executeUpdate();
} catch (Exception sqle) {
throw new DataAccessException(sqle.getMessage());
} finally {
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
public MemberBean selectMember(String id)
throws DataAccessException{
Connection con = null;
PreparedStatement pstmt = null;
try {
StringBuffer query = new StringBuffer();
query.append("select * from member where id=?");
DataSource dataSource=ServiceLocator.getInstance().getDataSource("jdbc/myoracle");
con = dataSource.getConnection();
pstmt = con.prepareStatement(query.toString());
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery();
MemberBean member=null;
if(rs.next()){
member=new MemberBean();
member.setId(rs.getString("id"));
member.setPw(rs.getString("pw"));
member.setAddr(rs.getString("addr"));
member.setTel(rs.getString("tel"));
}
return member;
} catch(Exception sqle) {
throw new DataAccessException(sqle.getMessage());
} finally {
try{
if(pstmt!=null){pstmt.close(); pstmt=null;}
if(con!=null){con.close(); con=null; }
}catch(Exception e){throw new RuntimeException(e.getMessage());}
}
}
public void deleteMember(String id) throws DataAccessException {
Connection con = null;
PreparedStatement pstmt = null;
try {
StringBuffer query = new StringBuffer();
query.append("delete from member where id=?");
DataSource dataSource = ServiceLocator.getInstance().getDataSource(
"jdbc/myoracle");
con = dataSource.getConnection();
pstmt = con.prepareStatement(query.toString());
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery();
} catch (Exception sqle) {
throw new DataAccessException(sqle.getMessage());
} finally {
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
}
MemberDAO.java
===================================================================
package kr.co.seoulit.member.dao;
import java.util.List;
import kr.co.seoulit.common.dao.DataAccessException;
import kr.co.seoulit.member.to.MemberBean;
public interface MemberDAO {
public List<MemberBean> selectMemberList() throws DataAccessException;
}
ServiceLocatorException.java
==============================================================
package kr.co.seoulit.common.sl;
public class ServiceLocatorException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ServiceLocatorException(String message) {
// TODO Auto-generated constructor stub
super(message);
}
}
ServiceLocator.java
===============================================================
package kr.co.seoulit.common.sl;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ServiceLocator {
private Map<String,DataSource> cache;
private Context envCtx;
private static ServiceLocator instance;
static {
try {
instance = new ServiceLocator();
} catch (ServiceLocatorException e) {
e.printStackTrace();
}
}
private ServiceLocator() {
try {
envCtx = new InitialContext();
cache = Collections.synchronizedMap(new HashMap<String,DataSource>());
} catch (Exception e) {
e.printStackTrace();
throw new ServiceLocatorException(e.getMessage());
}
}
public static ServiceLocator getInstance() {
return instance;
}
public DataSource getDataSource(String jndiName) throws ServiceLocatorException {
DataSource dataSource;
try {
if (cache.containsKey(jndiName)) {
dataSource=cache.get(jndiName);
} else {
dataSource = (DataSource)envCtx.lookup("java:comp/env/"+jndiName);
cache.put(jndiName, dataSource);
}
} catch (NamingException e) {
throw new ServiceLocatorException(e.getMessage());
}
return dataSource;
}
}
DataAccessException.java
================================================================
package kr.co.seoulit.common.dao;
@SuppressWarnings("serial")
public class DataAccessException extends RuntimeException {
public DataAccessException(){
super();
}
public DataAccessException(String message){
super(message);
}
public DataAccessException(Throwable cause){
super(cause);
}
public DataAccessException(String message, Throwable cause){
super(message, cause);
}
}
db1.jsp
======================================================
<%@page import="kr.co.seoulit.member.to.MemberBean"%>
<%@page import="java.util.List"%>
<%@page import="kr.co.seoulit.member.dao.MemberDAOImpl"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
td {border:3px solid navy; }
</style>
</head>
<body>
<form name=form1>
<table>
<%
List<MemberBean> list=MemberDAOImpl.getInstance().selectMemberList();
for(MemberBean bean:list){
out.println("<tr>");
out.println("<td><input type=radio name=rd_btn value="+bean.getId()+">"+"</td>");
out.println("<td>"+bean.getId()+"</td>");
out.println("<td>"+bean.getPw()+"</td>");
out.println("<td>"+bean.getAddr()+"</td>");
out.println("<td>"+bean.getTel()+"</td>");
out.println("</tr>");
}
%>
</table>
<input type="submit" value="수정" onclick="javascript_:document.form1.action='db3.jsp';">
<input type="submit" value="삭제" onclick="javascript_:document.form1.action='db2.jsp';">
</form>
</body>
</html>
db2.jsp
=============================================================
<%@page import="kr.co.seoulit.member.dao.MemberDAOImpl"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%=request.getParameter("rd_btn") %>삭제됨
<%
String id=request.getParameter("rd_btn");
MemberDAOImpl.getInstance().deleteMember(id);
response.sendRedirect("db1.jsp");
%>
</body>
</html>
db3.jsp
================================================================
<%@page import="kr.co.seoulit.member.to.MemberBean"%>
<%@page import="kr.co.seoulit.member.dao.MemberDAOImpl"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="db4.jsp">
<%
String id=request.getParameter("rd_btn");
MemberBean bean=MemberDAOImpl.getInstance().selectMember(id);
String pw=bean.getPw();
String addr=bean.getAddr();
String tel=bean.getTel();
%>
<input type="text" name=id value=<%=id%>>
<input type="text" name=pw value=<%=pw%>>
<input type="text" name=addr value=<%=addr%>>
<input type="text" name=tel value=<%=tel%>>
<input type="submit" value="저장">
</form>
</body>
</html>
db4.jsp
===========================================================
<%@page import="java.lang.reflect.Member"%>
<%@page import="kr.co.seoulit.member.dao.MemberDAOImpl"%>
<%@page import="kr.co.seoulit.member.to.MemberBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String id=request.getParameter("id");
String pw=request.getParameter("pw");
String addr=request.getParameter("addr");
String tel=request.getParameter("tel");
MemberDAOImpl.getInstance().updateMember(id,pw,addr,tel);
response.sendRedirect("db1.jsp");
%>
</body>
</html>
'개발하자 > JSP&Servlet' 카테고리의 다른 글
emp테이블의 ename을 출력하고 클릭하면 상세내용나오게 하기 (0) | 2015.03.01 |
---|---|
한폼에 submit버튼 두개일 때 이동페이지 설정 (0) | 2015.03.01 |
emp테이블의 ename 출력하고 클릭하면 상세정보 나옴(dept테이블 조인) (0) | 2015.02.25 |
DB연동하기 (0) | 2015.02.25 |
URIEncoding-한글깨질때 설정방법 (0) | 2015.02.23 |