cmd 작업
cmd
// 시퀀스 생성
sqlplus scott / tiger
create sequence seq_sungtb_no;
--------------------------------------------
// 테이블 생성
create table sungtb (
no int primary key,
name varchar2(30) not null,
kor int default 0,
eng int default 0,
mat int default 0);
--------------------------------------------
// 제약조건 추가
alter table sungtb add constraint ck_sungtb_kor check(kor between 0 and 100);
alter table sungtb add constraint ck_sungtb_eng check(kor between 0 and 100);
alter table sungtb add constraint ck_sungtb_mat check(kor between 0 and 100);
--------------------------------------------
// 데이터 입력
insert into sungtb values(seq_sungtb_no.nextVal, '무송', 77, 88, 99);
insert into sungtb values(seq_sungtb_no.nextVal, '임송', 66, 77, 88);
insert into sungtb values(seq_sungtb_no.nextVal, '호연작', 78, 89, 90);
commit;
--------------------------------------------
// 데이터 확인
select * from sungtb;
NO NAME KOR ENG MAT
---------- ------------------------------ ---------- ---------- ----------
1 무송 88 88 99
2 임송 66 77 88
3 호연작 78 89 90
SQL> conn system
암호 입력:
연결되었습니다.
SQL> show user;
USER은 "SYSTEM"입니다
--------------------------------------------
// scott 사용자에 권한 부여
SQL> grant dba to scott;
권한이 부여되었습니다.
--------------------------------------------
// scott 사용자 접속
SQL> conn scott
암호 입력:
연결되었습니다.
--------------------------------------------
// view table 생성 / table sungtb에 저장된 데이터를 이용해 view로 원하는 형태로 만든다.
SQL> create or replace view sungtb_vw as
2 select s.*, (kor + eng + mat) tot,
3 round((kor + eng + mat) / 3.1) avg,
4 rank() over(order by (kor + eng + mat) DESC) rank
5 from (select * from sungtb) s
6 order by no;
뷰가 생성되었습니다.
SQL> commit;
커밋이 완료되었습니다.
--------------------------------------------
// view table 확인
SQL> select * from sungtb_vw;
NO NAME KOR ENG MAT
---------- ------------------------------ ---------- ---------- ----------
TOT AVG RANK
---------- ---------- ----------
1 무송 88 88 99
275 89 1
2 임송 66 77 88
231 75 3
3 호연작 78 89 90
257 83 2
new Dynamic Project > ScoreTest
WEB-INF > lib
commons-collections-3.2.1.jar
commons-dbcp-1.2.2.jar
commos-pool-1.4.jar
ojdbc14.jar
META-INF
context.xml
package : class
dao : SjDAO.java
service : DBservice.java
vo : SjVO.java
SjVO.java
package vo;
public class SjVO {
String name;
int kor, eng, mat, total, no, rank;
float avg;
public String getName() {
return name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public float getAvg() {
return avg;
}
public void setAvg(float avg) {
this.avg = avg;
}
}
SjDAO.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import vo.SjVO;
import service.DBService;
//같은 프로젝트 안에서 다른 패키지에 있는 클래스를 불러서 사용할 수 있다.
public class SjDAO {
static SjDAO single = null;
// _singleton 템플레이트에서 자동 생성
public static SjDAO getInstance() {
//생성되지 않았으면 생성
if (single == null)
single = new SjDAO();
//생성된 객체정보를 반환
return single;
}
// 출력
public List<SjVO> selectList() {
List<SjVO> list = new ArrayList<SjVO>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from sungtb_vw";
try {
//1.Connection얻어온다
conn = DBService.getInstance().getConnection();
//2.명령처리객체정보를 얻어오기
pstmt = conn.prepareStatement(sql);
//3.결과행 처리객체 얻어오기
rs = pstmt.executeQuery();
//4.DB로 전송(res:처리된 행의 수)
while (rs.next()) {
SjVO vo = new SjVO();
vo.setName(rs.getString("name"));
vo.setKor(rs.getInt("kor"));
vo.setEng(rs.getInt("eng"));
vo.setMat(rs.getInt("mat"));
vo.setNo(rs.getInt("no"));
vo.setTotal(rs.getInt("tot"));
vo.setAvg(rs.getInt("avg"));
vo.setRank(rs.getInt("rank"));
list.add(vo);
}
} 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 list;
}
// 삭제
public int delete (int no) { // _insert_
int res = 0; // _insert_update 템플레이트에서 생성
Connection conn = null;
PreparedStatement pstmt = null;
// statement 라는 객체를 사용했지만 보안상 취약해서 PreaparedStatement를 사용
String sql = "delete from sungtb where no=?";
try {
//1.Connection획득
conn = DBService.getInstance().getConnection();
//2.명령처리객체 획득
pstmt = conn.prepareStatement(sql);
// sql query
pstmt.setInt(1, no);
// setInt(1,no) 1은 첫번째 sql ?에 넣는다는 말이고,
// int no 은 database에 title을 no으로 저장한 primary key이다.
//3.pstmt parameter 채우기
res = pstmt.executeUpdate();
//4.DB로 전송(res:처리된행수)
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return res;
}
// 값 추가 코드 생성
public int insert(SjVO vo) {
int res = 0;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into sungtb values(seq_sungtb_no.nextVal, ?,?,?,?)";
try {
conn = DBService.getInstance().getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, vo.getName());
pstmt.setInt(2, vo.getKor());
pstmt.setInt(3, vo.getEng());
pstmt.setInt(4, vo.getMat());
res = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) {pstmt.close();}
if(conn != null) {conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
return res;
}
// 값 변경 코드
public int update(SjVO vo) {
// TODO Auto-generated method stub
int res = 0;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update sungtb set name=?,kor=?,eng=?,mat=? where no=?";
try {
//1.Connection획득
conn = DBService.getInstance().getConnection();
//2.명령처리객체 획득
pstmt = conn.prepareStatement(sql);
//3.pstmt parameter 채우기
pstmt.setInt(5,vo.getNo() );
pstmt.setString(1, vo.getName() );
pstmt.setInt(2, vo.getKor());
pstmt.setInt(3, vo.getEng());
pstmt.setInt(4, vo.getMat());
//4.DB로 전송(res:처리된행수)
res = pstmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return res;
}
}
DBService.java
package service;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBService {
static DBService single = null;
public static DBService getInstance() {
if (single == null)
single = new DBService();
return single;
}
DataSource ds;
public DBService() {
try {
InitialContext ic = new InitialContext();
ds = (DataSource)ic.lookup("java:comp/env/jdbc/oracle_test");
} catch (NamingException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
Connection conn=null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
student.jsp
main 페이지
package vo;
public class SjVO {
String name;
int kor, eng, mat, total, no, rank;
float avg;
public String getName() {
return name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public float getAvg() {
return avg;
}
public void setAvg(float avg) {
this.avg = avg;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
아래 파일들은 기능 추가 jsp
sung_del.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="dao.SjDAO"%>
<%@ page import="vo.SjVO" %>
<!DOCTYPE html>
<%
request.setCharacterEncoding("utf-8");
int no = Integer.parseInt(request.getParameter("no"));
int res = SjDAO.getInstance().delete(no);
// delete() method가 없어서 오류
response.sendRedirect("student.jsp");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
sung_regi.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="vo.SjVO"%>
<%@ page import="dao.SjDAO"%>
<!DOCTYPE html>
<%
request.setCharacterEncoding("utf-8");
//전송버튼을 눌렀을 때 넘어온 파라미터를 받음
String name = request.getParameter("name");
int kor = Integer.parseInt(request.getParameter("kor"));
int eng = Integer.parseInt(request.getParameter("eng"));
int mat = Integer.parseInt(request.getParameter("mat"));
//받아온 정보를 vo에 저장
SjVO vo = new SjVO();
vo.setName(name);
vo.setKor(kor);
vo.setEng(eng);
vo.setMat(mat);
//DB에 값 추가, res가 0이면 실패 의미
int res = SjDAO.getInstance().insert(vo);
System.out.println(res);
//개싱된 데이터를 다시 student.jsp로 로딩함
response.sendRedirect("student.jsp");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
sung_update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="dao.SjDAO" %>
<%@ page import="vo.SjVO" %>
<!DOCTYPE html>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name2");
int no = Integer.parseInt(request.getParameter("no"));
int kor = Integer.parseInt(request.getParameter("kor2"));
int eng = Integer.parseInt(request.getParameter("eng2"));
int mat = Integer.parseInt(request.getParameter("mat2"));
SjVO vo = new SjVO();
vo.setNo(no);
vo.setName(name);
vo.setKor(kor);
vo.setEng(eng);
vo.setMat(mat);
int res = SjDAO.getInstance().update(vo);
System.out.println(res);
response.sendRedirect("student.jsp");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
JSP EL(Expression Language) (0) | 2022.08.25 |
---|---|
JSP ( 회원관리 DB 만들기 ) (0) | 2022.08.25 |
JSP Templates(템플릿 이용, 부서 별 사원 입력 및 출력) (0) | 2022.08.19 |
JSP (DB 연결, 부서 별 사원 입력 및 출력) (0) | 2022.08.19 |
VO(Value Object), JSP 내장 객체, (여러가지 정보를 하나로 묶어 저장할 수 있게 만든 클래스 VO) (0) | 2022.08.16 |