웹프로그래밍/JSP

JSP 댓글 게시판

폴더맨 2024. 6. 8. 10:37

JSP 댓글 게시판

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP MSSQL 연동 예제</title>
</head>
<body>
    <%
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // JDBC 드라이버 로드
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            // 데이터베이스 연결 설정
            String url = "jdbc:sqlserver://jspdb.database.windows.net:1433;databaseName=jsp_db1";
            String username = "유저이름"; // MSSQL 접속 계정
            String password = "비밀번호"; // MSSQL 접속 비밀번호
            // 데이터베이스 연결
            conn = DriverManager.getConnection(url, username, password);
            // 연결 성공 메시지 출력
            out.println("<h2>연결 성공</h2>");
            // SQL 쿼리 실행
            String sql = "SELECT * FROM comments";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            // 결과 출력
            out.println("<table border='1'>");
            out.println("<tr><th>제목</th><th>내용</th></tr>");
            while (rs.next()) {
                out.println("<tr>");
                out.println("<td>" + rs.getString("title") + "</td>");
                out.println("<td>" + rs.getString("contents") + "</td>");
                out.println("</tr>");
            }
            out.println("</table>");
        } catch (Exception e) {
            // 연결 실패 메시지 출력
            out.println("<h2>연결 실패</h2>");
            e.printStackTrace();
        } finally {
            // 리소스 해제
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        }
    %>
</body>
</html>

 

 

세상을 링크하라!!! 모든 링크를 한 자리에...
링크닷컴: https://linkdotcom.mycafe24.com/

 

유용한 링크, 링크닷컴 #무료 #링크사이트 #링크사이트

컴퓨터 및 일반 유용한 링크들을 모아 놓았습니다. 지금 바로 확인하세요!

linkdotcom.mycafe24.com