코딩 이야기

2023.03.23 jsp 수업정리 본문

DB

2023.03.23 jsp 수업정리

별메아리 2023. 3. 23. 11:16
728x90
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 목록</title>
</head>
<body>
<%
	// JDBC 드라이브 로딩(mysql)
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = null;
	// 연결하기
	// 드라이브 매니저에게 Connection 객체를 달라고 요청한다.
	// Connection을 얻기 위해 필요한 데이터(url,id,비밀번호)
	// String url = "jdbc:mysql://localhost:3306/데이터 베이스명?serverTimezone=Asia/Seoul";
	try{
		String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
		String dbUser = "root";
		String dbPass = "1234";
	
		DriverManager.getConnection(jdbcDriver,dbUser,dbPass);
		
		out.println("DB 연결 성공");
	} catch (SQLException ex){
		out.println("DB 연결 실패");
	}finally{
		if(conn!=null){conn.close();}
	}
%>
</body>
</html>

여기 까지가 DB 연결

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 목록</title>
</head>
<body>
	<table border=1>
		<tr>
			<td>게시판번호</td>
			<td>제목</td>
			<td>작성자</td>
			<td>작성일</td>
			<td>조회</td>
			<td>좋아요</td>
		</tr>
		<tr>
			<td>1</td>
			<td>제목입니다.</td>
			<td>정자바</td>
			<td>2023-03-23</td>
			<td>11</td>
			<td>2</td>
		</tr>
		<%
			// JDBC 드라이브 로딩(mysql)
			Class.forName("com.mysql.jdbc.Driver");
			// java api에 db를 연결하기 위한 객체
			Connection conn = null;
			//select의 결과를 DBMS에서 java로 저장하기 위한 객체
			ResultSet rs = null;
			// sql문장을 작성하고, 그sql문장을 실행하기 위한 객체
			Statement stmt = null;

			// 연결하기
			// 드라이브 매니저에게 Connection 객체를 달라고 요청한다.
			// Connection을 얻기 위해 필요한 데이터(url,id,비밀번호)
			// String url = "jdbc:mysql://localhost:3306/데이터 베이스명?serverTimezone=Asia/Seoul";
			try {
				String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
				String dbUser = "root";
				String dbPass = "1234";

				String query = "select * from board2";

				conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);

				stmt = conn.createStatement();
				// workbench 에서 ctrl+enter(쿼리 문장실행)을 통해 도출된 select결과를 ResultSet에 저장
				rs = stmt.executeQuery(query);

				while (rs.next()) {
		%>
		<tr>
			<td><%=rs.getInt("no")%></td>
			<td><%=rs.getString("title")%></td>
			<td><%=rs.getString("id")%></td>
			<td><%=rs.getString("regdate")%></td>
			<td><%=rs.getInt("count")%></td>
			<td><%=rs.getInt("good")%></td>
		</tr>
		<%
				}
			out.println("DB 연결 성공");
			} catch (SQLException ex) {
				out.println("DB 연결 실패");
			} finally {
				if (conn != null) {
					conn.close();
				}
			}
		%>
	</table>
</body>
</html>

DB에서 데이터를 갖고와서 반복문을 사용해 데이터가ture값이 있는 것만 가져옴

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 목록</title>
</head>
<body>
    <table border=1>
        <tr>
            <td>게시판번호</td>
            <td>제목</td>
            <td>작성자</td>
            <td>작성일</td>
            <td>조회</td>
            <td>좋아요</td>
        </tr>
        <%
            // JDBC 드라이브 로딩(mysql)
            Class.forName("com.mysql.jdbc.Driver");
            // java api에 db를 연결하기 위한 객체
            Connection conn = null;
            //select의 결과를 DBMS에서 java로 저장하기 위한 객체
            ResultSet rs = null;
            // sql문장을 작성하고, 그sql문장을 실행하기 위한 객체
            PreparedStatement pstmt = null;

            // 연결하기
            // 드라이브 매니저에게 Connection 객체를 달라고 요청한다.
            // Connection을 얻기 위해 필요한 데이터(url,id,비밀번호)
            // String url = "jdbc:mysql://localhost:3306/데이터 베이스명?serverTimezone=Asia/Seoul";
            try {
                String jdbcDriver = "jdbc:mysql://localhost:3306/sample?serverTimezone=Asia/Seoul";
                String dbUser = "root";
                String dbPass = "1234";

                String query = "select * from board2";
                conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
                pstmt = conn.prepareStatement(query);
                // workbench 에서 ctrl+enter(쿼리 문장실행)을 통해 도출된 select결과를 ResultSet에 저장
                rs = pstmt.executeQuery();

                if (rs.next()) {
        %>
        <tr>
            <td><%=rs.getInt("no")%></td>
            <td><%=rs.getString("title")%></td>
            <td><%=rs.getString("id")%></td>
            <td><%=rs.getString("regdate")%></td>
            <td><%=rs.getInt("count")%></td>
            <td><%=rs.getInt("good")%></td>
        </tr>
        <%
                }
            out.println("DB 연결 성공");
            } catch (SQLException ex) {
                out.println("DB 연결 실패");
            } finally {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            }
        %>
    </table>
</body>
</html>
728x90

'DB' 카테고리의 다른 글

20230317수업내용  (0) 2023.03.17
er 다이어 그램  (1) 2023.03.17
mysql 공부정리(4)  (0) 2023.03.16
논리삭제  (0) 2023.03.15
mysql 공부정리(3)  (0) 2023.03.15
Comments