일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 아이폰
- iPhone
- Android SDK
- Eclipse
- IO
- 개행처리
- MVC
- 자바
- 예제
- 이클립스
- 스프링
- ibatis 개행
- Android
- 외래키
- Oracle
- ibatis bind
- zipcode
- Objective C
- jdbc
- 우편번호
- Spring
- 연동
- 설치
- IT·컴퓨터
- ibatis parameter
- 오라클
- SQL
- 안드로이드
- java
- SEQUENCE
- Today
- Total
MisoBoy Blog...
[20110720] JDBC 를 통한 DB 연동 본문
JDBC 란? Java Database Connectivity의 약자로 자바를 이용하여 데이터베이스에 접근하여 각종 SQL문을
수행할 수 있도록 제공하는 API를 말합니다.
- 응용프로그램에서는 SQL문 만들어 JDBC Interface를 통해 전송하면 실제 구현 클래스인
JDBC 드라이버에서 DBMS에 접속을 시도하여 SQL문을 전송하게 됩니다.
- DBMS의 결과를 JDBC Driver와 JDBC Interface에게 전달되고 이를 다시 응용프로그램으로 전달 되어
SQL문의 결과를 볼 수 있습니다.
- JDBC의 역할은 Application과 DBMS의 Bridge 역할을 하게 됩니다.
JDBC 를 이용한 데이터베이스 연결 순서
1 단계 : import java.sql.*;
2 단계 : 드라이버 로드
3 단계 : Connection 객체를 생성
4 단계 : Statement 객체를 생성
5 단계 : SQL문에 결과물이 있다면 ResultSet 객체를 생성
6 단계 : 모든 객체를 닫음
Eclipse 에 JDBC Driver 설정 하기
Eclipse 에서 JDBC 연동을 하기 위해서는 해당 DB 프로그램의 JDBC Driver 를 연동 해야 합니다.
설정하는 방법은 아래와 같이 간단히 설정 해줄 수 있습니다.
JDBC Class 만들기
JDBC 를 연동 하기 위해 매번 같은 DB 접속문을 사용 할 필요가 없습니다.
간단히 해결하기 위해 생성자를 이용해 메서드를 호출하는 식으로 DB 접속문을 사용 할 수 있습니다.
사용 예제는 아래와 같습니다.
아래 예제 코드는 데이타베이스 프로그램별로 JDBC 를 연동 해줄 수 있습니다.
단, dbinfo 에 대한 정보는 propertise 파일을 만들어야 합니다.
JDBC 사용하기
import java.sql.SQLException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DBBean { private Connection conn; private Properties info; private Statement stmt; private PreparedStatement pstmt; private ResultSet rs; //Constructor public DBBean(String rdbms) throws IOException{ File file = null; switch(rdbms.toUpperCase().charAt(0)){ case 'O': //Oracle file = new File("D:\\temp\\dbinfo.properties"); break; case 'M': //MySQL file = new File("D:\\temp\\mysqlinfo.properties"); break; default : } info = new Properties(); info.load(new FileInputStream(file)); } public void loadDriver() throws ClassNotFoundException { Class.forName(this.info.getProperty("DBDRIVER")); } public void setConnection() throws SQLException { this.conn = DriverManager.getConnection(this.info.getProperty("DBURL"), this.info.getProperty("DBID"), this.info.getProperty("DBPWD")); } public Connection getConnection() throws SQLException { return this.conn; } public void setStatement() throws SQLException { this.stmt = conn.createStatement(); } public Statement getStatement() throws SQLException{ return this.stmt; } public void setPreparedStat(String sql) throws SQLException{ this.pstmt = conn.prepareStatement(sql); } public PreparedStatement getPreparedStatement() throws SQLException{ return this.pstmt; } public ResultSet executeSelect(String sql) throws SQLException { //SELECT rs = this.stmt.executeQuery(sql); return this.rs; } public int executeNonSelect(String sql) throws SQLException { int rowcount = 0; //UPDATE, DELETE, INSERT, DDL, DCL rowcount = this.stmt.executeUpdate(sql); return rowcount; } public void connClose() throws SQLException{ if(this.conn != null) this.conn.close(); } public void stmtClose() throws SQLException { if(this.stmt != null) this.stmt.close(); } public void rsClose() throws SQLException { if(this.rs != null) this.rs.close(); } }
'Java' 카테고리의 다른 글
Swing 을 이용한 DB 연동 프로그램 (0) | 2011.07.21 |
---|---|
[20110720] ResultSetMetaData 객체를 사용하여 출력하는 예제 (0) | 2011.07.20 |
[20110715] Java JDBC (JDBC 로 ORACLE 연동하여 ZIPCODE 프로그램) (0) | 2011.07.15 |
FileOutputStream 클래스를 이용해서 바이트 데이터를 파일에 쓰는 프로그램 (0) | 2011.07.12 |
FileWriter 클래스를 이용하여 문자 데이터를 파일에 쓰는 프로그램 (0) | 2011.07.11 |