[java] 이것이 자바다 ch20 데이터베이스 입출력 1(JDBC, Oracle SQL, INSERT)
bobo122023. 2. 14. 18:17
728x90
1. JDBC 개요
◎ JDBC 라이브러리
▷ 자바는 데이터베이스(DB)와 연결해서 데이터 입출력 작업을 할 수 있도록 JDBC 라이브러리 (java.sql 패키지)를 제공 ▷JDBC는 데이터베이스 관리시스템(DBMS)의 종류와 상관없이 동일하게 사용할 수 있는 클래스와 인터페이스로 구성
◎JDBC Driver ▷JDBC 인터페이스를 구현한 것으로, DBMS마다 별도로 다운로드받아 사용 ▷DriverManager 클래스: JDBC Driver를 관리하며 DB와 연결해서 Connection 구현 객체를 생성 ▷Connection 인터페이스: Statement, PreparedStatement, CallableStatement 구현 객체를 생성하며,
트랜잭션 처리 및 DB 연결을 끊을 때 사용 ▷Statement 인터페이스: SQL의 DDL과 DML 실행 시 사용 ▷PreparedStatement: SQL의 DDL, DML 문 실행 시 사용. 매개변수화된 SQL 문을 써 편리성과 보안성 유리 ▷CallableStatement: DB에 저장된 프로시저와 함수를 호출 ▷ResultSet: DB에서 가져온 데이터를 읽음
2. Oracle SQL 문법
1. 테이블 생성 방법
-- 사용자 USER 테이블
create table users(
userid varchar2(50) primary key,
username varchar2(50) not null,
userpassword varchar2(50) not null,
userage number(3) not null,
useremail varchar2(50) not null
);
-- 게시판 boards 테이블
create table boards(
bno number primary key,
btitle varchar2(100) not null,
bcontent clob not null,
bwriter varchar2(50) not null,
bdate date default sysdate,
bfilename varchar2(50) null,
bfiledata blob null
);
-- boards 테이블의 bno 값을 제공하는 시퀀스를 생성
create sequence SEQ_BNO nocache;
control + enter를 사용하면 작성한 구문이 실행됩니다. sequence는 mySQL의 auto_increment와 같이 자동으로 숫자를 증가시키는 기능을 합니다.
한줄 복사 : ctrl + shift + d 한줄 삭제 : alt + shift + d
2. 계좌 정보 테이블 생성 및 데이터 입력
-- 계좌 정보 저장될 accounts 테이블 생성
create table accounts(
ano varchar(20) primary key,
owner varchar(20) not null,
balance number not null
);
-- 계좌 정보 데이터 입력
insert into accounts(ano, owner, balance)
values ('111-111-1111', '하여름', 1000000);
insert into accounts(ano, owner, balance)
values ('222-222-2222', '한겨울', 0);
select * from accounts;
commit;
3. 사용자 정보를 저장하는 프로시저 생성
-- 새로운 사용자 정보를 저장하는 프로시저
create or replace procedure user_create(
a_userid in users.userid%type,
a_username in users.username%type,
a_userpassword in users.userpassword%type,
a_userage in users.userage%type,
a_useremail in users.useremail%type,
a_rows out pls_integer
-- 사용자 정보를 저장하고 나서 1개의 행을 삽입했다는 의미로
-- 1을 out타입 매개변수에 저장
)
is
begin
insert into users (userid, username, userpassword, userage, useremail)
values (a_userid, a_username, a_userpassword, a_userage, a_useremail);
a_rows := sql%rowcount;
commit;
end;
/
4. user 테이블 생성 및 확인 결과를 리턴
-- users 테이블에서 userid와 userpassword 확인 그 결과를 리턴하는 user_login() 함수 생성
create or replace function user_login(
a_userid users.userid%type,
a_userpassword users.userpassword%type
) return pls_integer
is
v_userpassword users.userpassword%type;
v_result pls_integer;
begin
select userpassword into v_userpassword
from users
where userid = a_userid;
if v_userpassword = a_userpassword then
return 0; -- id랑 password 일치 : 0 반환
else
return 1; -- id랑 password 불일치 : 1 반환
end if;
exception
when no_data_found then
return 2; -- userid가 없으면 2 반환
end;
/
3. 데이터베이스 연결 ▷ 클라이언트 프로그램에서 DB와 연결하려면 해당 DBMS의 JDBC Driver가 필요 ▷① DBMS가 설치된 컴퓨터의 IP 주소, ② DBMS가 허용하는 포트(Port) 번호,
1. users 테이블에 새로운 사용자 정보를 저장하는 INSERT 문 실행 2. INSERT 문을 String 타입 변수 sql에 문자열로 대입
3. 매개변수화된 SQL 문을 실행하기 위해 Connection의 prepareStatement() 메소드로부터 PreparedStatement를 얻음
4. 값을 지정한 후 executeUpdate() 메소드를 호출하면 SQL 문이 실행되면서 users 테이블에 1개의 행이 저장
5. close() 메소드를 호출하면 PreparedStatement가 사용했던 메모리 해제