Java

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

에띠 2022. 4. 21. 11:24
728x90
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBC4 {
    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("update tb_member set ")
                .append("mem_userpw = '" + userpw + "', ")
                .append("mem_name = '" + name + "', ")
                .append("mem_hp = '" + hp + "', ")
                .append("mem_email = '" + email + "', ")
                .append("mem_hobby = '" + hobby + "', ")
                .append("mem_ssn1 = '" + ssn1 + "', ")
                .append("mem_ssn2 = '" + ssn2 + "', ")
                .append("mem_zipcode = '" + zipcode + "', ")
                .append("mem_address1 = '" + address1 + "', ")
                .append("mem_address2 = '" + address2 + "', ")
                .append("mem_address3 = '" + address3 + "' ")
                .append("where mem_userid = '" + userid + "'");
        System.out.println(sql);
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, id, pw);
            Statement stmt = conn.createStatement();
            int result = stmt.executeUpdate(sql.toString());
            if (result >= 1) System.out.println("회원정보 수정이 완료되었습니다.");
            else System.out.println("회원정보 수정 실패했습니다.");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}

변경 전
변경 후

 

728x90