본문

151220P(일)

Java - 21강 JDBC 프로그래밍

 

객체지향 데이터베이스

데이터와 기능을 함께 저장, 참조

 

MySQL 서버 시작

start mysqld-nt

 

MySQL 서버 실행 확인

mysqladmin -u root ping

 

MySQL 서버 종료

mysqladmin -u root shutdown

 

DB 만들기

mysqladmin -u root create shindb

 

DB 삭제

mysqladmin -u root drop shindb

 


 

MySQL 프로그램 시작

mysql -u root

 

특정 데이터베이스 사용

use shindb

 

table 만들기

create table goodsinfo (

code char(5) not null,

name varchar(30) not null,

price int(8) not null,

maker varchar(20),

primary key(code)

);

 

데이터 저장

insert into goodsinfo(code, name, price, maker) values('10001', '디지털 TV', 350000, 'LG');

 

데이터 수정

컬럼이름:=새로운값

update goodsinfo set name:='컬러액정 전자사전', price:=300000 where code='10004';

 

데이터 삭제(테이블은 삭제안됨)

delete from goodsinfo where code='10005';

 

테이블 삭제

drop table goodsinfo;

 


 

MySQL 프로그램 종료

quit

 

패스워드 설정

mysqladmin -u root password "1234"

 

패스워드 설정 후 실행

mysqladmin -u root ping -p

mysql -u root -p

 


 

JDBC driver

JDBC API 와 DBMS 연결

Mysql : connector/J

 

DB에 데이터를 넣고 꺼낼 때 단계 및 과정

1. JDBC Driver를 JVM안으로 Read = JDBC Driver Load

2. DB Connect

3. DB에 data write & read

4. DB Disconnect

 

1. Dirver Load

Class.forName("com.mysql.jdbc.Dirver");

com.mysql.jdbc.Dirver : JDBC 클래스 이름

 

jdbc:mysql//219.153.12.14:3306/shindb

jdbc : protocol

mysql : subprotocol

219.153.12.14:3306 : subname(IP:Port)

 

subname

네트워크의 수많은 컴퓨터 중에서 특정 DB 서버의 특정 DB를 찾음

 

2. Connection

Connection conn = DriverManager.getConnection("jdbc:mysql//219.153.12.14:3306/shindb", "richman", "money");

DB URL, 사용자 ID, 패스워드

 

4. Disconnection

conn.close();

 

localhost

자기자신의 IP주소를 가리킴

 

연결과정은 try - catch 문으로 감싸준다.

catch(ClassNotFoundException cnfe) {

}

catch(SQLException se) {

}

 

Table Data Read

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select code, num from goodsinfo;");

 

행단위로 read - 다음행으로~

boolean exist = rs.next();

while(rs.next()) {
}

 

String code  = rs.getString("code");

int price = rs.getInt("price");

 

rs.close(); 로 ResultSet 객체를 반드시 끝맺음 해준다.

stmt.close(); 로 Statement 객체도 닫는다.

 

MySQL : ISO-8859-1 Charactor set 사용

Java : Unicode Charactor set 사용

∴ 한글이 깨진다.

byte[] b = str.getBytes("ISO-8859-1");

return new String(b);

 

executeSelect, executeUpdate, executeDelete...

 


 

'Programming > Java' 카테고리의 다른 글

160117P(일)  (0) 2016.01.17
160109P(토)  (0) 2016.01.10
160108P(금)  (0) 2016.01.09
160107P(목)  (0) 2016.01.07
160103P(일)  (0) 2016.01.04

공유

댓글