1. Main문 작성 -> 2.InsertForm작성 -> 3.Insert작성
=============================================================================================================
Main문을 통해 구문을 실행 하게 된다.
위 사진과 같이 학생명단을 출력 시켜주는 구문인다.
=============================================================================================================
우선 작성 하는 부분은 HTML 부분을 작성 하게 된다.
HTML작성후 <% %> 부분에 코드 작성을 통해 전체 테이블에 값을 불러올수 있게 된다.
성적관리(JSP)구문 작성은 크게 제목이 있고 링크를 통한 페이지 이동이 있다.
<body>영역에 작성하게 되며
<body>
<div>
첫번쨰 <div>부분작성
<div class = "title"> ->class식별자를 지정하여 title이름을 지정해준다.
<h1>성적관리(JSP)</h1> ->위 사진에서 보는것과 같이 큰 제목이 되게 된다.
<p>
<a herf = "StudentMain.jsp">[학생관리]</a> -><a>태그는 링크를 걸어 해당 페이지(StudentMain.jsp)로 이동시켜준다.
<a herf = "GradeMain.jsp">[성적관리]</a>
</p>
</div>
=============================================================================================================
두번쨰 <div>부분작성
<div calss="main"> ->class식별자를 지정하여 main이름을 지정해준다.
<h2>--학생명단--</h2>
<form>
<input type = "button" value = "학생추가" onclick ="location.href='StudentInsertForm.jsp'">
-> 위에 input type = "butoon"을 통해서 학생추가 페이지로 이동할수 있게 된다.
</form>
<p>전체 학새우 : <%= count>명 </p> -> 전체학생수를 읽어올떄 사용하게 된다.
->일단 가양식으로 만들 경우 <p>전체 학생수 : 2명 </p>로 작성하게 된다. 2명 ->count부분이 된다.
->count를 불러오는 내용은 위 <% %>부분에 작성하게 된다.. 밑에서 확인..
<table>
<tr>
<th>번호</th>
<th>이름</th>
<th>전화번호</th>
<th>성적 입력 여부</th>
</tr> ->테이블의 카테고리에 해당되는 부분이다. th를 통해 이름 강조!
------------------------------
<가양식작성>
<%--<tr>
<td>1</td><td>Kim</td><td>010-111-1111</td><td>O</td>
</tr>--%>
------------------------------
<%= sb.toString()%>
-> <% %>부분에 코드 작성을 통해 테이블을 입력받아 저장할수 있게 된다.
</table>
</div>
</div>
</body>
=============================================================================================================
두번쨰 작성 <% %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "com.test.*%>
<%
StringBuilder sb = new StringBuilder(); -> 테이블에 값을 Builder를 통해 쌓았다 한번에 출력하게 된다
int count = 0; -> 인원수 출력시 사용!
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try{
Conn = DBConn.getConnection();
String sql1 = String.format("select count(*) as \"count\" from studentView"); ->count 구문!
String sql2 = String.format("select sid, name, tel, sub from studentView"); ->학생명단 출력!
pstmt1 = conn.prepareStatement(sql1);
pstmt2 = conn.prepareStatement(sql2);
ResultSet rs1 = pstmt1.executeQuery();
ResultSet rs2 = pstmt2.executeQuery();
while(rs1.next()){ ->전체 인원수 받아올경우 사용
count = rs1.getInt("count");
}
rs1.close();
while(rs2.next()){ ->테이블 구성요소를 sb.append에 한줄에 하나씩 입력!
sb.append(String.format("<tr>\r\n"));
sb.append(String.format("<td>%s</td>",rs2.getString("sid") ));
sb.append(String.format("<td>%s</td>",rs2.getString("name") ));
sb.append(String.format("<td>%s</td>",rs2.getString("tel") ));
sb.append(String.format("<td>%s</td>",(Integer.parseInt(rs2.getString("sub"))==null)?"X":"O" ));
sb.append(String.format("</th>\r\n));
}
rs2.close();
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt1 != null) {
pstmt1.close();
}
if (pstmt2 != null){
pstmt2.close();
}
} catch (Exception e) {
}
DBConn.close();
}
%>
=============================================================================================================
전체 코드
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.test.*"%>
<%
//학생 명단 저장
StringBuilder sb = new StringBuilder();
//인원수 저장
int count = 0;
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
conn = DBConn.getConnection();
//인워수 출력쿼리
String sql1 = String.format("select count(*) as \"count\" from studentView");
//학생 명단 출력 쿼리
String sql2 = String.format("select sid, name, tel, sub from studentView");
pstmt1 = conn.prepareStatement(sql1);
pstmt2 = conn.prepareStatement(sql2);
ResultSet rs1 = pstmt1.executeQuery();
ResultSet rs2 = pstmt2.executeQuery();
while(rs1.next()){
count = rs1.getInt("count");
}
rs1.close();
while(rs2.next()){
sb.append(String.format("<tr>\r\n"));
sb.append(String.format("<td>%s</td>",rs2.getString("sid")));
sb.append(String.format("<td>%s</td>",rs2.getString("name")));
sb.append(String.format("<td>%s</td>",rs2.getString("tel")));
sb.append(String.format("<td>%s</td>",(Integer.parseInt(rs2.getString("sub"))==0)?"X":"O"));
sb.append(String.format("</tr>\r\n"));
}
rs2.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt1 != null) {
pstmt1.close();
}
if (pstmt2 != null){
pstmt2.close();
}
} catch (Exception e) {
}
DBConn.close();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>성적관리</title>
<link rel="stylesheet" type="text/css" href="TableStyle.css">
<link rel="stylesheet" type="text/css" href="DivStyle.css">
</head>
<body>
<div>
<%-- 메인 메뉴 영역 --%>
<div class="title">
<h1>성적관리(JSP버전)</h1>
<p>
<a href="StudentMain.jsp">[학생관리]</a> <a href="GradeMain.jsp">[성적관리]</a>
</p>
</div>
<%-- 콘텐츠 영역 --%>
<div class="main">
<h2>--학생명단--</h2>
<form>
<input type="button" value="학생추가"
onclick="location.href='StudentInsertForm.jsp'">
</form>
<%-- <p>전체 학생 수 : 2명</p> --%>
<p>전체 학생수 : <%=count%>명</p>
<table id="t01" style = "text-align : center;">
<tr>
<th style="text-align: center;">번호</th>
<th>이름</th>
<th>전화번호</th>
<th>성적입력여부</th>
</tr>
<!-- <tr style="text-align: center;">
<td>1</td>
<td>Kim gildong</td>
<td>010-1234-1234</td>
<td>X</td>
</tr> -->
<%= sb.toString()%>
</table>
</div>
</div>
</body>
</html>
'JSP' 카테고리의 다른 글
StudentInsert (0) | 2015.05.17 |
---|---|
StudentInsertForm (1) | 2015.05.15 |
Insert작성방법 (0) | 2015.05.15 |
InsertForm 작성방법 (0) | 2015.05.15 |
Main작성방법 (1) | 2015.05.15 |