programming/jsp

jsp-oracle연결해서 사용

히연쓰 2021. 4. 29. 15:24

먼저, 나는 heeyeon이라는 아이디와 비밀번호 1234를 이용하여 AAA계정을 생성하였다.

그리고나서 MEMBER라는 테이블을 만들고 NAME, ID, PASSWORD, EMAIL, PHONE 컬럼들을 생성하였다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <div>
        <form action="insert_member_action03.jsp" method="POST">
            <div>
                <label>
                    <span>이름</span>
                    <input type="text" name="userName">
                </label>
            </div>
            <div>
                <label>
                    <span>아이디</span>
                    <input type="text" name="userID">
                </label>
            </div>
            <div>
                <label>
                    <span>패스워드</span>
                    <input type="text" name="userPw">
                </label>    
            </div>
            <div>
                <label>
                    <span>이메일</span>
                    <input type="text" name="email">
                </label>    
            </div>
            <div>
                <label>
                    <span>전화번호</span>
                    <input type="text" name="phone">
                </label>    
            </div>
            <div>
                <input type="submit" value="회원가입">
                <input type="reset" value="취소">
            </div>
        </form>
    </div>
</body>
</html>
 
cs

insert_member_03.jsp 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%!
    //전역 변수 설정하기
    //멤버 변수 도는 멤버 메서드 설정하는 곳
    Connection con = null;
    PreparedStatement pstmt = null;
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String id = "heeyeon";
    String pw = "1234";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        String userID = request.getParameter("userID");
        String userPw = request.getParameter("userPw");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        try{
            Class.forName(driver);
            con = DriverManager.getConnection(url, id, pw);
            System.out.println("접속 성공");
            String sql = "INSERT INTO MEMBER VALUES (?,?,?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1,userName);
            pstmt.setString(2,userID);
            pstmt.setString(3,userPw);
            pstmt.setString(4,email);
            pstmt.setString(5,phone);
            
            int result = pstmt.executeUpdate();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(pstmt!=null) pstmt.close();
                if(con!=null) con.close();
            } catch(Exception e2) {
                e2.printStackTrace();
            }
        }
    %>
    <a href="member_list.jsp">전체회원보기</a>
    
</body>
</html>
 
cs

insert_member_action03.jsp 파일

insert_member_03.jsp

 

insert_member_action03.jsp 

이러한 화면이 나와 나의 정보를 입력할 수 있다.

그리고 회원가입을 누르면 전체회원보기 라는 링크가 걸려있는 화면이 나온다

 

oracle안에도 내가 입력한 정보가 들어가 있는 것을 알 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%!Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
 
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String id = "heeyeon";
    String pw = "1234";
    %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 
    <div>
        <table
            style="width: 800px; border: 1px solid #333; text-align: center;">
            <thead>
                <tr>
                    <th>name</th>
                    <th>id</th>
                    <th>password</th>
                    <th>email</th>
                    <th>phone</th>
                </tr>
            </thead>
            <tbody>
                <%
                try {
                    Class.forName(driver);
                    con = DriverManager.getConnection(url, id, pw);
                    System.out.println("접속");
                    String sql = "SELECT * FROM MEMBER";
                    pstmt = con.prepareStatement(sql);
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                        out.println("<tr>");
                        out.println("<td>"+rs.getString("name")+"</td>");
                        out.println("<td><a href='member_info.jsp?id="+rs.getString("id")+"'>"+rs.getString("id")+"</a></td>");
                        out.println("<td>"+rs.getString("password")+"</td>");
                        out.println("<td>"+rs.getString("email")+"</td>");
                        out.println("<td>"+rs.getString("phone")+"</td>");
                        out.println("</tr>");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if(rs!=null) rs.close();
                        if(pstmt!=null) pstmt.close();
                        if(con!=null) con.close();
                    } catch(Exception e2) {
                        e2.printStackTrace();
                    }
                }
                %>
            </tbody>
        </table>
    </div>
 
</body>
</html>
 
cs

member_list.jsp 파일.

member_list.jsp 결과 -> 으 지저분하게도 색칙해놨다..

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%!
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
 
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:xe";
    String id = "heeyeon";
    String pw = "1234";%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Member info</h1>
 
    <table>
        <thead>
            <tr>
                <th>name</th>
                <th>id</th>
                <th>pw</th>
                <th>email</th>
                <th>phone</th>
            </tr>
        </thead>
        <tbody>
            <%
            try {
                String pid = request.getParameter("id");
                Class.forName(driver);
                con = DriverManager.getConnection(url, id, pw);
                //System.out.println("성공");
                String sql = "SELECT * FROM MEMBER WHERE ID = ?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, pid);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    out.println("<tr>");
                    out.println("<th>"+rs.getString("name")+"</th>");
                    out.println("<th>"+rs.getString("id")+"</th>");
                    out.println("<th>"+rs.getString("password")+"</th>");
                    out.println("<th>"+rs.getString("email")+"</th>");
                    out.println("<th>"+rs.getString("phone")+"</th>");
                    out.println("</tr>");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try{
                    if(rs!=null) rs.close();
                    if(pstmt!=null) pstmt.close();
                    if(con!=null) con.close();
                } catch(Exception e2) {
                    e2.printStackTrace();
                }
            }
            %>
 
 
        </tbody>
    </table>
</body>
</html>
cs

member_info.jsp

 

id에 링크가 걸려 있는 것을 눌러 확인해보면

 

member_info.jsp의 결과

결론 정말 정말 어렵다 ......................

노력해야징 ............

'programming > jsp' 카테고리의 다른 글

jsp - summernote 이용하기2  (0) 2021.05.11
jsp - summernote이용하기  (4) 2021.05.11
JSP-자유게시판  (0) 2021.05.04
jsp 회원가입  (2) 2021.05.03
그냥 내가 잘 못해서 까먹을까봐 적어놓은 oracle빈 테이블 만들기  (2) 2021.04.29