Java

[Java] Statement 인터페이스를 이용한 Table insert

에띠 2022. 4. 21. 11:22
728x90

Statement 인터페이스
- Connection 클래스의 createStatement() 메소드로 호출
- Statement 객체는 Statement 인터페이스를 구현한 객체
- executeUpdate() 메소드를 통해 insert, update, delete SQL구문을 실행할 수 있음
- executeQuery() 메소드를 통해 select SQL구문을 실행할 수 있음

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBC3 {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1/aiclass?useSSL=false";
        String id = "root";
        String pw = "1234";

        String userid, userpw, name, hp, email, hobby, ssn1, ssn2, zipcode, address1, address2, address3;
        Scanner sc = new Scanner(System.in);
        System.out.println("********* 회원가입 **********");
        System.out.print("아이디를 입력하세요 : ");
        userid = sc.next();
        System.out.print("비밀번호를 입력하세요 : ");
        userpw = sc.next();
        System.out.print("이름을 입력하세요 : ");
        name = sc.next();
        System.out.print("연락처를 입력하세요 : ");
        hp = sc.next();
        System.out.print("이메일을 입력하세요 : ");
        email = sc.next();
        System.out.print("취미를 입력하세요 : ");
        hobby = sc.next();
        System.out.print("주민등록번호 앞자리를 입력하세요 : ");
        ssn1 = sc.next();
        System.out.print("주민등록번호 뒷자리를 입력하세요 : ");
        ssn2 = sc.next();
        System.out.print("우편번호를 입력하세요 : ");
        zipcode = sc.next();
        System.out.print("주소를 입력하세요 : ");
        address1 = sc.next();
        System.out.print("상세주소를 입력하세요 : ");
        address2 = sc.next();
        System.out.print("참고사항을 입력하세요 : ");
        address3 = sc.next();
        StringBuilder sql = new StringBuilder();
        sql.append("insert into tb_member(mem_userid, mem_userpw, mem_name, " +
                    "mem_hp, mem_email, mem_hobby, mem_ssn1, mem_ssn2, mem_zipcode," +
                    " mem_address1, mem_address2, mem_address3) values ('")
                .append(userid + "', '").append(userpw + "', '").append(name + "', '")
                .append(hp + "', '").append(email + "', '").append(hobby + "', '")
                .append(ssn1 + "', '").append(ssn2 + "', '").append(zipcode + "', '")
                .append(address1 + "', '").append(address2 + "', '").append(address3 + "')");
        System.out.println(sql);

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, id, pw);
            // Statement : SQL 구문을 실행할 수 있게 해주는 인터페이스
            Statement stmt = conn.createStatement();
            // executeUpdate() : 몇개의 row가 실행되었는지 반환.
            int result = stmt.executeUpdate(sql.toString());
            if (result >= 1)  {
                System.out.println("회원가입이 완료되었습니다.");
            }
            else System.out.println("회원가입 실패했습니다.");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}

 

728x90