MisoBoy Blog...

[20110720] JDBC 를 통한 DB 연동 본문

Java

[20110720] JDBC 를 통한 DB 연동

misoboy 2011. 7. 20. 09:27

JDBC 란? Java Database Connectivity의 약자로 자바를 이용하여 데이터베이스에 접근하여 각종 SQL문을

수행할 수 있도록 제공하는 API를 말합니다.


- JDBC는 크게 JDBC 인터페이스와 JDBC 드라이버로 구성되어 있습니다.
 



응용프로그램에서는 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();
	}
}