CustomerProc.java
- package cus;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Vector;
import cus.CustomerBean;
public class CustomerProc extends HttpServlet {
String url = "jdbc:oracle:thin:@127.0.0.1:1521:cjedu";
String user = "scott";
String passwd = "tiger";
public CustomerProc(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
public Vector selectAll(){
Connection con;
Statement stat;
CustomerBean c;
Vector result = new Vector();
try {
con = DriverManager.getConnection( url, user, passwd );
stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM customer ORDER BY num";
ResultSet rs = stat.executeQuery(sql);
while ( rs.next() ) {
String num = rs.getString(1);
String name = rs.getString(2);
String addr = rs.getString(3);
c = new CustomerBean(num, name, addr);
result.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public int insertData(String sql){
Connection con;
Statement stat;
int t=0;
try {
con = DriverManager.getConnection( url, user, passwd );
stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
t = stat.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public int deleteData(String sql){
Connection con;
Statement stat;
int t = 0;
try {
con = DriverManager.getConnection( url, user, passwd );
stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
t = stat.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public Vector searchData(String sql){
Connection con;
Statement stat;
CustomerBean c;
Vector result = new Vector();
try {
con = DriverManager.getConnection( url, user, passwd );
stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stat.executeQuery(sql);
while ( rs.next() ) {
String num = rs.getString(1);
String name = rs.getString(2);
String addr = rs.getString(3);
c = new CustomerBean(num, name, addr);
result.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
CustomerBean.java
- //data bean : data 입출력을 위한 데이터 출력
package cus;
public class CustomerBean {
private String num = "" ;
private String name = "";
private String address = "";
public CustomerBean() {
}
public CustomerBean(String num, String name, String address){
this.num = num;
this.name = name;
this.address = address;
}
public void setNum(String num){
this.num = num;
}
public String getNum(){
return num;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
}
index.jsp
- <%@ page contentType="text/html; charset=euc-kr" %>
<HTML><HEAD><TITLE> Index.JSP Start your mark!!! </TITLE></HEAD>
<style type="text/css">
body {background-color:#e0e0e0; text-align:center;}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8;}
table {
</style>
<BODY>
<h1> JSP Process Test 를 시작합니다.</h1>
<h3> 원하는 항목을 선택하여 주세요. </h3>
<hr>
<form method=post action="process.jsp"><table border=1 align=center width=>
<tr><th width=50>선택</th><th>작업내용</th><tr>
<tr><td align=center><INPUT TYPE="radio" NAME="choice" value="select"></td><td>전체항목보기("SELECT * FROM customer")</td></tr>
<tr><td align=center><INPUT TYPE="radio" NAME="choice" value="insert"></td><td>새항목 입력하기("INSERT INTO customer VALUES(~~~)")</td></tr>
<tr><td align=center><INPUT TYPE="radio" NAME="choice" value="delete"></td><td>선택된 항목 삭제하기("DELETE customer WHERE num IN(~~~)")</td></tr>
<tr><td align=center><INPUT TYPE="radio" NAME="choice" value="search"></td><td>검색을 희망하는 항목을 입력하여 찾기</td></tr>
</table><input type=submit value="실행"><input type=reset value="취소"></form>
</BODY>
</HTML>
process.jsp(jst:forward 기능 수행)
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page errorPage="errorPage.jsp" %>
<HTML><HEAD><TITLE> JSP Process Test Forward Page </TITLE>
</HEAD>
<BODY>
<!-- index.jsp 에서 radio 의 속성값을 알아오자. -->
<%
String value = request.getParameter("choice");
if ( value.equals("select")){
%>
<jsp:forward page="select.jsp" />
<%
} else if ( value.equals("insert") ){
%>
<jsp:forward page="insert.jsp" />
<%
} else if ( value.equals("delete") ){
%>
<jsp:forward page="delete.jsp" />
<%
} else if ( value.equals("search") ){
%>
<jsp:forward page="Search.jsp" />
<%
}
%>
</BODY>
</HTML>
select.jsp
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import = "java.sql.*, java.util.*" %>
<%@ page errorPage="errorPage.jsp" %>
<jsp:useBean id="cus" class="cus.CustomerProc" scope="page" />
<HTML><HEAD><TITLE> JSP use Bean on JDBC </TITLE></HEAD>
<style type="text/css">
body {background-color:#e0e0e0}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8; text-align:center;}
</style>
<BODY><center>
<h1>JDBC Bean Use Result</h1><hr>
<%
Vector v = cus.selectAll();
if ( v.isEmpty() != false ){
out.println("현재 DB에서 넘겨받은 데이터가 없습니다.");
//out.println(v.size());
} else {
%>
<table border=1 align=center width=500>
<tr><th>번호</th><th>이름</th><th>주소</th>
<%
for( int i = 0 ; i < v.size() ; i++){
cus.CustomerBean cu = (cus.CustomerBean)v.elementAt(i);
String num = cu.getNum();
String name = cu.getName();
String addr = cu.getAddress();
%>
<tr><td><%= num %></td><td><%= name %></td><td><%= addr %></td></tr>
<%
}
}
%>
</table><br>
<a href="index.jsp">초기 화면으로</a>
</BODY>
</HTML>
insert.jsp(입력폼을 통해 입력된 자료를 insertProc.jsp에 전달한다)
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page errorPage="errorPage.jsp" %>
<HTML> <HEAD> <TITLE> Insert data to customer table. </TITLE>
</HEAD>
<style type="text/css">
body {background-color:#e0e0e0; text-align:center;}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8;}
table {
</style>
<BODY>
<h1>INSERT DATA TO CUSTOMER TABLE</h1>
<hr>
<form method=post action="InsertProc.jsp">
<table border=1 width=500 align=center>
<tr><th width=60>항목</th><th>입력 항목</th><tr>
<tr><th>번호</th><td><input type=text name='num' size=60></td><tr>
<tr><th>이름</th><td><input type=text name='name' size=60></td><tr>
<tr><th>주소</th><td><input type=text name='addr' size=60></td><tr>
</table><input type=submit value='입력'><input type=reset value='재작성'><a href="Index.jsp">초기 화면으로</a>
</form>
</BODY>
</HTML>
insertProc.jsp
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page errorPage="errorPage.jsp" %>
<HTML><HEAD><TITLE> INSERT process Page </TITLE></HEAD>
<jsp:useBean id="cp" class="cus.CustomerProc" scope="page" />
<style type="text/css">
body {background-color:#e0e0e0}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8; text-align:center;}
</style>
<BODY><Center>
<%
String num = request.getParameter("num");
String name = request.getParameter("name");
String addr = request.getParameter("addr");
String sql = "INSERT INTO customer Values('" + num + "', '" +name + "','" + addr + "')";
int result = cp.insertData(sql);
if ( result == 0 ) {
%>
<h1>데이터가 정상적으로 입력되지 않았습니다.</h1><hr>
입력하신 데이터와 관련한 오류가 발생하였습니다.
입력하신 내용을 확인해주세요.<br><br>
입력내용 : <%= sql %> <br><br>
<a href="Insert.jsp">다시 입력 화면으로</a>
<%
} else {
%>
<h1>데이터가 정상적으로 입력되었습니다.</h1>
<a href="Index.jsp">초기 선택 화면으로</a>
<%
}
%>
</center>
</BODY>
</HTML>
자료가 정상적으로(무결성 오류없이) 처리되었을 때.
DB에 존재하지 않는 검색 사항을 입력했을 경우
검색 데이터를 입력하지 않았을 경우 자바스크립트로 실행차단함
Delete.jsp
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import = "java.sql.*, java.util.*" %>
<%@ page errorPage="errorPage.jsp" %>
<jsp:useBean id="cus" class="cus.CustomerProc" scope="page" />
<HTML><HEAD><TITLE> JSP use Bean on JDBC </TITLE></HEAD>
<style type="text/css">
body {background-color:#e0e0e0}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8; text-align:center;}
</style>
<BODY><center>
<h1>JDBC Bean Use Result</h1><hr>
<%
Vector v = cus.selectAll();
if ( v.isEmpty() != false ){
out.println("현재 DB에서 넘겨받은 데이터가 없습니다.");
//out.println(v.size());
} else {
%>
<form method=post action='DeleteProc.jsp'>
<table border=1 align=center width=500>
<tr><th>check</th><th>번호</th><th>이름</th><th>주소</th>
<%
for( int i = 0 ; i < v.size() ; i++){
cus.CustomerBean cu = (cus.CustomerBean)v.elementAt(i);
String num = cu.getNum();
String name = cu.getName();
String addr = cu.getAddress();
%>
<tr><td><input type=checkbox name=num value=<%= num %>></td><td><%= num %></td><td><%= name %></td><td><%= addr %></td></tr>
<%
}
%>
<tr><td colspan=4><input type=submit value='삭제하기'><input type=reset value='재선택'></td></tr>
</table></form>
<%
}
%>
<br>
<a href="index.jsp">초기 화면으로</a>
</BODY>
</HTML>
deleteProc.jsp
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page errorPage="errorPage.jsp" %>
<jsp:useBean id="cp" class="cus.CustomerProc" scope="page" />
<HTML><HEAD><TITLE> DELETE process page </TITLE></HEAD>
<style type="text/css">
body {background-color:#e0e0e0}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8; text-align:center;}
</style>
<BODY><center>
<%!
String sql = "DELETE customer WHERE num IN ('";
%>
<%
String[] values=request.getParameterValues("num");
if (values != null){
for(int i = 0; i < values.length; i++) {
sql += values[i];
if ( i == values.length-1){
sql += "'";
} else {
sql += "','";
}
}
sql += ")";
} else {
sql = "선택하신 항목이 없습니다.";
}
int t = cp.deleteData(sql);
%>
<h1> 삭제를 위해 선택한 자료들은 다음과 같습니다.</h1>
<%
if(values != null){
%>
<table border=1 width=500>
<tr><th>선택하신 데이터번호</th></tr><tr><td>
<%
for(int i = 0; i < values.length; i++) {
%>
<%= values[i] %>
<%
}
%>
</td></tr></table>
<%
}
%>
<hr>
선택하신 <%= t %> 개의 자료는 삭제되었습니다.<br><br>
<a href=index.jsp %>초기 화면으로</a>
</center></BODY>
</HTML>
search.jsp
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page errorPage="errorPage.jsp" %>
<jsp:useBean id="cus" class="cus.CustomerProc" scope="page" />
<HTML><HEAD><TITLE> Search Process JSP Page </TITLE></HEAD>
<style type="text/css">
body {background-color:#e0e0e0}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8; text-align:center;}
</style>
<script type='text/javascript'>
var checkArticle = function checkArticle(){
var num = document.form1.article.value.length;
if ( num === 0 ) {
alert("데이터를 입력해주세요.");
return false;
} else {
return true;
}
}
</script>
<BODY><center>
<h1>검색을 희망하시는 항목을 선택하신 후 데이터를 입력해주세요.</h1>
<hr>
<form name=form1 method=post action='SearchProc.jsp' onSubmit='return checkArticle()'><table border=1 width=500>
<tr><th>항목 선택</th><th>원하는 검색 데이터 입력</th></tr>
<tr><td><select name='attribute'>
<option value='num'>번 호
<option value='name'>이 름
<option value='address'>주 소
</select>
</td><td><input type=text name='article' size=55></td></tr>
<tr><td colspan=2><input type=submit value='검색'> <input type=reset></td></tr>
</table></form>
</center></BODY>
</HTML>
searchProc.jsp
- <%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import = "java.sql.*, java.util.*" %>
<%@ page errorPage="errorPage.jsp" %>
<jsp:useBean id="cus" class="cus.CustomerProc" scope="page" />
<HTML><HEAD><TITLE> This page is Search Result Return </TITLE></HEAD>
<style type="text/css">
body {background-color:#e0e0e0}
th { background-color:#404040; color:#f8f8f8}
td { background-color:#A0A0A0; color:#f8f8f8; text-align:center;}
</style>
<BODY><center>
<h1> 요청하신 검색의 결과입니다. </h1><hr>
<%
String attribute = request.getParameter("attribute");
String article = request.getParameter("article");
String sql = "SELECT * FROM customer where " + attribute + " like '%"+article+"%' order by num";
%>
<h3> 검색 Query : <%= sql %> </h3><hr>
<%
Vector v = cus.searchData(sql);
if ( v.isEmpty() == true ){
out.println("<h2>현재 DB에서 넘겨받은 데이터가 없습니다.</h2>");
//out.println(v.size());
} else {
%>
<table border=1 align=center width=500>
<tr><th>번호</th><th>이름</th><th>주소</th>
<%
for( int i = 0 ; i < v.size() ; i++){
cus.CustomerBean cu = (cus.CustomerBean)v.elementAt(i);
String num = cu.getNum();
String name = cu.getName();
String addr = cu.getAddress();
%>
<tr><td><%= num %></td><td><%= name %></td><td><%= addr %></td></tr>
<%
}
%>
</table>
<%
}
%>
<br><a href='index.jsp'>초기 화면으로</a>
</center></BODY>
</HTML>
검색 항목을 입력하지 않고 출력하려고 할 경우 발생함
입력한 검색 결과가 없을 경우 나타나는 화면
정상적으로 입력되었을 경우 나타나는 화면
이 글은 스프링노트에서 작성되었습니다.
'Java > Language' 카테고리의 다른 글
Javaone : Change (Y)our World : 우리의 세상을 바꿀 수 있을까? (0) | 2009.06.03 |
---|---|
EL과 JSTL (0) | 2009.06.01 |
09/05/22 쿠키 (0) | 2009.05.25 |
허니몬, JavaFX 예제 만들기 (0) | 2009.05.24 |
허니몬, DTFE, JavaFX 세미나에 다녀오다. ^^ (0) | 2009.05.24 |