ABOUT ME

  • JDBC
    JAVA 2021. 7. 24. 22:47

    객체 지향

    자바의 객체지향

    객체( 상태=객체의 상태 + 행동) & 상태(독립상태= 인스턴스변수로 선언하지 않고 메서드 내에서 지역변수로 선언한 것들.)

    C언어와 객체지향 차이

    C언어:

    상태 (값=변수) , 행동(함수)

    객체가 없고 상태 따로 행동 따로 다룸.

    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
    //회원객체를 위한 설명서
    class 회원 { // 레코드(튜플), 레코드가 여러개 모이면 표.
        int 번호; // 필드
        String 성명;
        String 주민번호;
    }
     
    public class App {
        
        public static void 회원등록하다(회원 새회원) {
            try {
                // DB연결관리객체: Connection, 명령전달객체: Statement, 질의결과관리객체: ResultSet
                Class.forName("com.mysql.cj.jdbc.Driver");
     
                // 1. 드라이버관리자야 연결자를 줄래, 연결자는(mydb1인 DB를 가지고 올, id, pass)인 MySQL의 DB연결관리자야,
                Connection DB연결관리자 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1?useUnicode=true",
                        "root""1234");
     
                // 2. DB연결관리자 통해 명령전달자를 얻는다.
                Statement 명령전달자 = DB연결관리자.createStatement();
                System.out.println("연결 성공");
     
                // 3. 명령하기
                String 삽입SQL = "insert into 회원(성명,주민번호) values('" + 새회원.성명 + "','" + 새회원.주민번호 + "');";
                명령전달자.executeUpdate(삽입SQL); // 명령전달자가 보낸다.
     
                // 4. 연결끊기
                DB연결관리자.close();
     
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        public static void main(String[] args) {
            회원 새회원 = new 회원();
            새회원.성명 = "라길동";
            새회원.주민번호 = "777";
     
            회원등록하다(새회원);
            System.out.println("등록되었다.");
        }
     
    }
     
    //MySQL, mydb1 데이터베이스에 있는 회원테이블에 성명,주민번호가 등록된다.
    cs

    <body>
    회원등록<br>
    <form action= "등록.jsp" method="POST">
    
    성명<input type = "text" name = "name"><br>
    주민번호<input type="text" name ="pnumber"><br>
    
    <input type = "submit" name = "등록">
    
    </form>
    </body>
        <%
        request.setCharacterEncoding("UTF-8");
        //요청처리객체야 넘어온 데이터중 이름이 "name" 인 것 줘라.
        String 성명 = request.getParameter("name");
        String 주민번호 = request.getParameter("pnumber");
        
        System.out.println(성명);
        System.out.println(주민번호);
        %>

    jsp에 메서드를 넣을 때 library클래스들은 import해와야한다.(윗설명 아래예시 참조)

    다운받은 library는 이 동적프로그램안에 없고 java프로젝트에만 넣었으니

    동적프로그램 : WebContent - WEB-INF - lib 안에다가 lib폴더를 복사해서 넣으면 끝난다.(톰캣이 관리하는 폴더)

    javaproject는 읽는 기능이 없어 저번에 설정을 따로 했지만 톰캣은 자동으로 해석하기 때문에 아무것도 안해도 된다.

    이제 import로 다운받은 library를 쓸 수 있다.


    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %>
        
        <%!
        //<%! : 메서드 정의, 행동설명//
        public static void 회원등록하다(String 성명, String 주민번호) {
    		try {
    			// DB연결관리객체: Connection, 명령전달객체: Statement, 질의결과관리객체: ResultSet
    
    			// 0. 드라이버로드. 다운받은 mysql-library에 있는 Driver가져오면 된다.
    			Class.forName("com.mysql.cj.jdbc.Driver");
    
    			// 1. 드라이버관리자를 통해 DB연결자를 얻는다.(이미 연결된 상태로)
    			// 드라이버관리자야 연결자를 줄래, 연결자는(mydb1인 DB를 가지고 올, id, pass)인 MySQL의 DB연결관리자야,
    			// DB연결관리자 = Connection인터페이스 익명객체이다.
    			Connection DB연결관리자 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1?useUnicode=true",
    					"root", "1234");
    
    			// 2. DB연결관리자 통해 명령전달자를 얻는다.
    			// 명령전달자 = Statement인터페이스 익명객체이다.
    			Statement 명령전달자 = DB연결관리자.createStatement();
    
    			System.out.println("연결 성공");
    			// 3. 명령하기
    			String 삽입SQL = "insert into 회원(성명,주민번호) values('" + 성명 + "','" + 주민번호 + "');";
    			명령전달자.executeUpdate(삽입SQL); // 명령전달자가 보낸다.
    
    			// 4. 연결끊기
    			// DB연결관리자야 연결 끊어
    			DB연결관리자.close();
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    
    	}
        
        %>
      
        <%
        //요청처리객체야 넘어온 데이터를 UTF-8로 인코딩 해줘.(안 그러면 다른걸로 넘어와서 한글깨짐)
        request.setCharacterEncoding("UTF-8");
        //요청처리객체야 넘어온 데이터중 이름이 "name" 인 것 줘라.
        String 성명 = request.getParameter("name");
        String 주민번호 = request.getParameter("pnumber");
        
        회원등록하다(성명,주민번호);
        
        %>
      
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	등록되었습니다.
    	
    </body>
    </html>

    'JAVA' 카테고리의 다른 글

    댓글

Designed by Tistory.