기록해! 정리해!

자바 - jsp 오라클 연결 (학생목록보기) 본문

JAVA/JSP

자바 - jsp 오라클 연결 (학생목록보기)

zsuling 2022. 7. 8. 16:15

1. 오라클에 붙혀넣고 커밋하기

 

Create table examtbl (
 sno varchar(10)  not  null primary key ,
 sname varchar(20),
 kor  int,
 eng  int,
 math int,
 hist int
) ;

insert into examtbl values('10101','김학생', 75, 85, 90, 60);
insert into examtbl values('10102','이학생', 70, 75, 70, 60);
insert into examtbl values('10103','박학생', 70, 85, 80, 75);
insert into examtbl values('10201','조학생', 90, 85, 100, 50);
insert into examtbl values('10202','황학생', 65, 65, 60, 70);
insert into examtbl values('10203','임학생', 45, 55, 50, 55);
insert into examtbl values('10304','천학생', 70, 75, 85, 80);
insert into examtbl values('10305','남학생', 100, 85, 90, 90);
insert into examtbl values('10306','여학생', 80, 95, 90, 85);
insert into examtbl values('10307','노학생', 35, 55, 70, 55);

select *  from examtbl;

 

2. webapp에 list.jsp만들기

3. 테이블만들기

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>학생 성적 목록보기</title>
</head>
<body>
<div align=center>
<table border=1>
<tr><td>학번</td><td>이름</td>
<td>국어</td><td>영어</td><td>수학</td><td>역사</td></tr>
</table>
</div>
</body>
</html>

4. DBconn하기

index.php에서 복붙하고 수정하기

<%
String url = "jdbc:oracle:thin:@//localhost:1521/xe";
String id = "system";
String pwd = "1234";

Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(url,id,pwd);

String SQL = "select * from examtbl";
PreparedStatement pstmt = conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
%>

5. 학생성적목록보기

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String url = "jdbc:oracle:thin:@//localhost:1521/xe";
String id = "system";
String pwd = "1234";

Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(url,id,pwd);

String SQL = "select * from examtbl";
PreparedStatement pstmt = conn.prepareStatement(SQL);
ResultSet rs = pstmt.executeQuery();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>학생 성적 목록보기</title>
</head>
<body>
<div align=center>
<table border=1>
<tr><td>학번</td><td>이름</td>
<td>국어</td><td>영어</td><td>수학</td><td>역사</td><td>합계</td><td>평균</td></tr>

<%
while(rs.next()){
int sumK = rs.getInt("kor")+rs.getInt("eng")+rs.getInt("math")+rs.getInt("hist");
double avgK = sumK/4.0;
%>

<tr>
<td><%= rs.getString("sno") %></td>
<td><%= rs.getString("sname") %></td>
<td><%= rs.getInt("kor") %></td>
<td><%= rs.getInt("eng") %></td>
<td><%= rs.getInt("math") %></td>
<td><%= rs.getInt("hist") %></td>
<td><%= sumK %></td>
<td><%= avgK %></td>
</tr>
<% } %>
</table>
</div>
</body>
</html>

 

'JAVA > JSP' 카테고리의 다른 글

서블릿 (1)  (0) 2022.07.19
Export/Import/Servlet/Dao  (0) 2022.07.19
자바 - java EE 복습  (0) 2022.07.11
자바 - Bean 학생목록조회 list.jsp  (0) 2022.07.08
자바 -jsp연결  (0) 2022.07.08
Comments