본문 바로가기
JSP

JSP(표만들기)_2024-09-03

by 앵보몬 2024. 9. 4.
728x90
반응형

JSP(서블릿 페이지)는 동적인 웹 콘텐츠를 생성하기 위해 사용되는 기술입니다. JSP를 이용해 표를 만들기 위해서는 HTML을 사용해서 표의 구조를 정의하고, 필요에 따라 JSP의 스크립트릿을 사용해 데이터를 동적으로 삽입할 수 있습니다.

① HTML 테이블의 기본 구조

<table border="1">
    <thead>
        <tr>
            <th>헤더 1</th>
            <th>헤더 2</th>
            <th>헤더 3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>데이터 1-1</td>
            <td>데이터 1-2</td>
            <td>데이터 1-3</td>
        </tr>
        <tr>
            <td>데이터 2-1</td>
            <td>데이터 2-2</td>
            <td>데이터 2-3</td>
        </tr>
    </tbody>
</table>

 

② JSP에서 동적으로 테이블 생성하기

<%@ page import="java.util.*" %>
<%
    // 예제 데이터를 준비합니다.
    List<List<String>> data = new ArrayList<>();
   
    // 첫 번째 행 데이터
    List<String> row1 = Arrays.asList("데이터 1-1", "데이터 1-2", "데이터 1-3");
    data.add(row1);
   
    // 두 번째 행 데이터
    List<String> row2 = Arrays.asList("데이터 2-1", "데이터 2-2", "데이터 2-3");
    data.add(row2);
%>

<!DOCTYPE html>
<html>
<head>
    <title>JSP Table Example</title>
</head>
<body>
    <table border="1">
        <thead>
            <tr>
                <th>헤더 1</th>
                <th>헤더 2</th>
                <th>헤더 3</th>
            </tr>
        </thead>
        <tbody>
            <%
                for (List<String> row : data) {
                    out.print("<tr>");
                    for (String cell : row) {
                        out.print("<td>" + cell + "</td>");
                    }
                    out.print("</tr>");
                }
            %>
        </tbody>
    </table>
</body>
</html>

 

test15.jsp
0.00MB

 

DB

create table products (
    product_id number(4) primary key,
    product_name varchar2(20) not null,
    category varchar2(20),
    price number(10, 2),
    description varchar2(20),
    stock_quantity number(10, 2)
    );
   
insert into products values (1, '아이폰 15', '스마트폰', 1500000, '최신 스마트폰', 52000);
insert into products values (2, '삼성 갤럭시 s24', '스마트폰', 1400000, '고성능 스마트폰', 62000);
insert into products values (3, '맥북 프로 14', '노트북', 2500000, '고급형 노트북', 52000);
insert into products values (4, '삼성 노트북', '노트북', 1800000, '가성비 노트북', 62000);
insert into products values (5, '에어팟 프로 2세대', '이어폰', 350000, '무선 노이즈캔슬링 이어폰', 62000);
COMMIT;

 

test15.jsp

<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="common.JDBConnect"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="EUC-KR" />
    <title>상품재고조회</title>
  </head>
  <body>
    <h2>상품재고조회</h2>
    <table border="1">
      <thead>
        <tr>
          <th>Product_id</th>
          <th>Product_name</th>
          <th>Category</th>
          <th>Price</th>
          <th>Description</th>
          <th>Stock_quantity</th>
        </tr>
      </thead>
      <tbody>
    <%
   
     ResultSet rs = null;
     
    JDBConnect jdbc = new JDBConnect();
   
    try{
    String sql = "select * from products";
    jdbc.stmt = jdbc.con.createStatement();
    rs = jdbc.stmt.executeQuery(sql);
   
    while (rs.next()){
    %>
   
    <tr>
        <td><%= rs.getString("product_id") %></td>
        <td><%= rs.getString("product_name") %></td>
        <td><%= rs.getString("category") %></td>
        <td><%= rs.getString("price") %></td>
        <td><%= rs.getString("description") %></td>
        <td><%= rs.getString("stock_quantity") %></td>
    </tr>
    <%
    }
    }catch (SQLException e) {
       out.println(" 데이터 베이스 접속 에러");
    } finally {
       if (rs != null) rs.close();
        if (jdbc.con != null) jdbc.con.close();
    }
   %>
   
      </tbody>
    </table>    
</body>
</html>

 

728x90
반응형