IT_Programming/Java

JDBC를 위한 클래스

JJun ™ 2008. 8. 1. 07:25

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnector

{
     private static final String JDBC_DRIVER  = "JDBC 드라이버";
     private static final String JDBC_URL  = "DB RUL";
     private static final String JDBC_USERNAME = "사용자 아이디";
     private static final String JDBC_PASSWORD = "비밀번호";
 
     private Connection connection = null ; 
     private Statement statement = null ;
 
 /**
  *
  * @throws ClassNotFoundException
  * @throws SQLException
  */
 public DBConnector() throws ClassNotFoundException, SQLException {  
     this(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);
 }
 
 /**
  *
  * @param driver TODO
  * @param url
  * @param user
  * @param pass
  * @throws ClassNotFoundException
  * @throws SQLException
  */
 public DBConnector(String driver, final String url, final String user, final String pass) throws ClassNotFoundException, SQLException {
  Class.forName(driver);  
  setConfiguration(url, user, pass);
 }
 
 
/**
  *
  * @param jdbcDriver TODO
  * @param url
  * @param user
  * @param pass
  * @throws SQLException
  */
 public final void setConnection(String jdbcDriver, final String url, final String user, final String pass) throws ClassNotFoundException, SQLException {
  Class.forName(jdbcDriver);
  setConfiguration(url, user, pass);
 }
 
 /**
  *
  * @param url
  * @param user
  * @param pass
  * @throws SQLException
  */
 private final void setConfiguration(final String url, final String user, final String pass) throws SQLException {
  connection = DriverManager.getConnection(url, user, pass);  
  statement = connection.createStatement();
 }
 
 
/**
  *
  * @param sql
  * @return
  * @throws SQLException
  */
 public final ResultSet executeQuery(String sql) throws SQLException {
  return statement.executeQuery(sql);
 }
 
 /**
  *
  * @param sql
  * @return
  * @throws SQLException
  */
 public final int executeUpdate(String sql) throws SQLException {
  return statement.executeUpdate(sql);
 }

 

/**
  * 커넥션 객체를 close 시킨다.
  * @throws SQLException
  */

 public final void DBConnectClose() throws SQLException
 {
    connection.close();
 }

}