oracle_select_option.txt
1. select 문
SELECT [DISTINCT] { *, column, [alias], , , }
FROM table_name
[WHERE conditions]
[ORDER bY {column, expression} [ ASC | DESC] ];
DISTINCT : 중복되는 행을 제거하는 옵션
* : 테이블의 모든 column을 출력
alias : 해당 column에 대해서 다른 이름을 부여 할때
table_name : 질의 대상 테이블 명
WHERE : 조건을 만족 하는 행들만 검색
conditions : column, 표현식, 상수 및 비교연산자
ORDER BY : 질의 결과 정렬을 위한 옵션 (ASC:오름차순(default), DESC:내림차순)
2. WHERE 절에 올 수 있는 연산자
BETWEEN a and b : a와 b 사이의 데이터를 출력(a, b 값 포함)
SELECT empno, ename FROM emp where sal BETWEEN 3000 AND 5000;
--> 급여가 3000에서 5000 사이인 사원만 출력
IN (list,,) : list의 값들 중 어느 하나와 일치하는 데이터 출력
SELECT empno, ename FROM emp where empno IN(7900, 7934);
--> 사번이 7900 또는 7934번인 사원의 사번과 이름 출력
LIKE : 문자 형태로 일치하는 데이터를 출력
" % " : 여러개의 문자열을 나타내는 와일드카드
SELECT empno, ename FROM emp where UPPER(ename) like '%K%';
--> 이름 중 'K' 문자가 들어가 있는 사원 정보 출력
" _ " : 단 하나자의 문자열을 나타내는 와일드카드
SELECT empno, ename FROM emp where UPPER(ename) like '_I%';
--> '_' 는 한문자, 이름 중 'I' 문자가 두번째에 위치한 사원 정보 출력
IS NULL : NULL 값을 가진 데이터를 출력
NOT BETWEEN a AND b : a와 b 사이에 있지 않은 데이터를 출력(a, b값 포함하지 않음)
NOT IN (list) : list와 일치 하지 않는 데이터를 출력
SELECT empno, ename FROM emp where empno NOT IN(7900, 7934);
--> 사번이 7900 또는 7934번이 아닌 사원의 사번과 이름 출력
NOT LIKE : 문자형태와 일치 하지 않는 데이터를 출력
NOT IS NULL : NULL 값을 갖지 않는 데이터를 출력
***********************************************************
select * from saram
select * from saram where age>10 and age<23 order by salary desc
같은의미 다른 명령어
select * from SARAM where age between 10 and 23 order by salary desc
> ASC : 오름차순 정렬, DESC : 내림차순 정렬
***********************************************************
session.setattribute("string",object) ------> 세션정보를 서버에 저장
***********************************************************
Cookie (교재 p.159 : 제 12강 Cookie&Session 참고)
Cookie ! 오픈마켓에서 오늘 본 품목을 옆에 띄워줄때 응용된다.
쿠키는 string 이름과 string value로 이루어져 있다.
session과 cookie의 차이
session은 서버에 저장,
cookie는 client에 저장
쿠키의 구성요소
1.이름 : 각각의 쿠키를 구별하는데 사용되는 이름
2.값 : 쿠키의 이름과 관련된 값
3.유효시간 : 쿠키의 유지시간
4.도메인 : 쿠키를 전송할 도메인
5.경로 : 쿠키를 전송할 요청자료
쿠키관련 메소드
반환형 |
메소드 |
설명 |
String |
getName() |
쿠키의 이름을 구한다 |
String |
getValue() |
쿠키의 값을 구한다 |
void |
setValue(String value) |
쿠키의 값을 지정한다 |
void |
setDomain(String pattern) |
이 쿠키가 전송될 서버의 도메인을 지정한다 |
String |
getDomain() |
쿠키의 도메인을 구한다 |
void |
setPath(String url) |
쿠키를 전송할 경로를 지정한다 |
String |
getPath() |
쿠키의 전송경로를 구한다 |
void |
setMaxAge(int expiry) |
쿠키의 유효시간을 초단위로 지정한다. 음수를 입력할 경우 웹 브라우저를 닫을 때 쿠키가 함께 삭제 된다 |
int |
getMaxAge() |
쿠키의 유효시간을 구한다 |
실습
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
String sing = "choa";
session.setAttribute("id", sing);
Cookie c = new Cookie("idol","iu");
c.setValue("exid");
c.setMaxAge(60*60*24*7);
response.addCookie(c);
%>
<%=c.getName() %> : 쿠키의 이름<br>
<%=c.getValue() %> : value of cookie
</body>
</html>
결과
idol : 쿠키의 이름
exid : value of cookie
***********************************************************
배열로담기
cookie1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
String sing = "choa";
session.setAttribute("id", sing);
Cookie c = new Cookie("idol","iu");
c.setValue("exid");
c.setMaxAge(60*60*24*7);
response.addCookie(c);
%>
<%=c.getName() %> : 쿠키의 이름<br>
<%=c.getValue() %> : value of cookie<br>
<a href="./cookie2.jsp">cookie2</a>
</body>
</html>
cookie2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
Cookie [] co = request.getCookies(); //배열로 얻어온다. getCookie's'()
for(int i=0;i<co.length;i++){ %><br>
쿠키의 이름 :<%=co[i].getName() %><br>
쿠키의 벨류 :<%=co[i].getValue() %>
<% }%>
%>
</body>
</html>
***********************************************************
세션, 쿠키가 남긴 데이터를 이용해서 분석
알리바바
그 넓은 영토에서 어떻게 하루만에 배송이 가능한가?
세션, 쿠키를 이용한 데이터 분석으로
미리 구매가 많이 일어나는 지점에 가져다 놓는다.
***********************************************************
삼성페이, 카카오페이에서 벌어들이는 수익은?
수수료, ? ===> 미미한 수익
데이터를 모으기 위함. ==> 미래를 예측
***********************************************************
게시판 만들기
작성자 , 내용, 제목, 번호
이중 프라이머리 키는 ?? ==> 번호 , 번호는 자동생성되도록,
일단 테이블 만들기
create table noticBoard(
no number(5) primary key,
writer varchar2(100),
title varchar2(100),
contents varchar2(500));
commit;
이클립스 스크랩북에서 입력
insert into noticBoard values (1,'admin','test1','test1')
insert into noticBoard values (2,'admin','test2','test2')
select * from noticBoard
select count(no) from noticBoard
>개수 세기 결과 count(no) : 2
count는 게시글의 갯수
1,2, 5
insert into noticBoard values (5,'admin','test5','test5')
select * from noticBoard
select count(no) from noticBoard
>게시글의 수 : 3을 리턴
select max(no) from noticBoard
>가장 큰 번호 : 5를 리턴
max는 번호 중에 가장 큰 수를 리턴;
이 경우에는 max를 사용하는 것이 낫다.
select count(no), max(no) from noticBoard
게시판폼
nWriteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<style type="text/css">
table{
}
</style>
</head>
<body>
<form action="./noticePro.jsp" method="post">
<table border="1">
<tr>
<td width="30%">제 목 : </td>
<td width="70%"><input type="text" name="title"><br></td>
</tr>
<tr>
<td>작성자 : </td>
<td><input type="text" name="writer" ><br></td>
</tr>
<tr>
<td colspan=2>
내 용 : <br>
<textarea rows="20" cols="80" name="contents" ></textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" value="등록">
<input type="reset" value="다시 작성">
</td>
</tr>
</table>
</form>
</body>
</html>
프로세스 noticePro.jsp
<%@page import="notice.NoticeBoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<jsp:useBean id="n" class="notice.NoticeBoardDTO"/>
<jsp:setProperty property="*" name="n"/>
<%
NoticeBoardDAO bd = new NoticeBoardDAO();
int num = bd.setNotice(n);
if(num>0){%>
등록완료<br>
<a href="list.jsp">리스트보기</a>
<%
}else{
response.sendRedirect("./nWriteForm.jsp");
}
%>
%>
</body>
</html>
자바DAO NoticeBoardDAO.java
package notice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
public class NoticeBoardDAO {
private String user = "user01";
private String password = "user01";
private String url="jdbc:oracle:thin:@192.168.116.128:1521:xe";
private String driver="oracle.jdbc.driver.OracleDriver";
private Connection con;
private PreparedStatement st;
private ResultSet rs;
public NoticeBoardDAO(){
try {
Class.forName(driver); //드라이버 로딩
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//리스트 꺼내오기...
public ArrayList<NoticeBoardDTO> getList(){
ArrayList<NoticeBoardDTO> ar = new ArrayList<>();
String sql = "select no, title, writer from noticBoard order by no desc";
//sql문 스크랩북에서 확인하기
try {
st = con.prepareStatement(sql);
rs = st.executeQuery();
while(rs.next()){
NoticeBoardDTO n = new NoticeBoardDTO();
n.setNo(rs.getInt("no"));
n.setTitle(rs.getString("title"));
n.setWriter(rs.getString("writer"));
ar.add(n);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ar;
}
//글쓰기 입력 메소드
public int setNotice(NoticeBoardDTO d){
int num = getNo();
String sql = "insert into noticBoard values(?, ?, ?, ?)";
try {
st = con.prepareStatement(sql);
st.setInt(1, num+1);
st.setString(2, d.getWriter());
st.setString(3, d.getTitle());
st.setString(4, d.getContents());
num = st.executeUpdate();
} catch (Exception e) {
num=0;
e.printStackTrace();
}
return num;
}
//글번호 최대값을 가져오는 메소드(1,2,5 번다음에 글쓰면 3이아니라 6이 오게해줌)
int getNo(){
String sql = "select max(no) from noticBoard";
int num=0;
try {
Statement st = con.createStatement();
rs = st.executeQuery(sql);
if(rs.next()){
num=rs.getInt(1);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
public static void main(String[] args) {
NoticeBoardDAO n = new NoticeBoardDAO();
System.out.println(n.getNo());
}
}
자바 DTO NoticeBoardDTO.java
package notice;
public class NoticeBoardDTO {
private int no;
private String writer;
private String title;
private String contents;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
}
리스트 list.jsp
<%@page import="notice.NoticeBoardDTO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="notice.NoticeBoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
NoticeBoardDAO nd = new NoticeBoardDAO();
ArrayList<NoticeBoardDTO> ar = nd.getList();
%>
<!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 사용해서 -->
<table border=1>
<tr>
<td>글번호</td>
<td>글제목</td>
<td>작성자</td>
</tr>
<%
for(int i=0;i<ar.size();i++){
%>
<tr>
<td><%=ar.get(i).getNo() %></td>
<td><%=ar.get(i).getTitle() %></td>
<td><%=ar.get(i).getWriter() %></td>
</tr>
<%} %>
</table>
</body>
</html>
***********************************************************
DAO : DB와 연결하는 클래스
DTO : 변수(접근지정자 private)와 게터세터 메소드로 이루어진 클래스
***********************************************************
get 방식 : 링크를 클릭하는 것은 전부 get 방식이다.
post 방식
***********************************************************
a링크로 파라미터 넘기기 ?파라미터이름=값
newFile.jsp
<%@page import="notice.NoticeBoardDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
NoticeBoardDTO n = new NoticeBoardDTO();
n.setNo(3);
%>
<a href="newFile2.jsp?n=<%=n.getNo()%>">test</a>
</body>
</html>
newFile2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
String name = request.getParameter("n");
%>
<%=name %>
</body>
</html>
***********************************************************
***********************************************************
***********************************************************