본문 바로가기

IT 서비스 바라보기/Programming

게시판 만들기 -4- (접속 부분 만들기)



이번엔 커넥션풀부분과 접속부를 당담하는 부분을 만들어 보자.


ConnectionFactory.java   -> 접속을 당담할 부분

4가지의 경우가 있다. odbc,oracle,mssql,mysql일때의 경우


-----------------------------------------------------------------------------

package mydb.poolfact ;
import java.sql.*;


public class ConnectionFactory {


  public static final int ODBC = 1;
  public static final int ORACLE = 2;
  public static final int MSSQL= 3;
  public static final int MYSQL = 4;

 // private String url = "192.168.123.55" ;
 // private String port = "1521";
 // private String dbname = "orcl";
 // private String dbuser = "orauser1" ;
 // private String pswd = "tiger";
 // private String unicode ="true" ;
 // private String encode = "euc_kr" ;

  private String url = "127.0.0.1" ;
  private String port = "3306";
  private String dbname = "jspdb";
  private String dbuser = "jspuser" ;
  private String pswd = "1234";
//  private String unicode ="true" ;
//  private String encode = "euckr" ;

  public ConnectionFactory() {}


  public Connection getConnection(int dbms)  throws SQLException {
    Connection conn = null;

    if (dbms == ConnectionFactory.ODBC) {
      try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        conn = DriverManager.getConnection("jdbc:odbc:dbdsn", "id", "password");
      }catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe);
      }
    }
 
 
 else if (dbms == ConnectionFactory.ORACLE) {
      try {
       Class.forName("oracle.jdbc.driver.OracleDriver");
       conn = DriverManager.getConnection("jdbc:oracle:thin:@"+url+":"+port+":"+dbname,dbuser,pswd);
      }catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe);
      }      
    }
 
 
 else if (dbms == ConnectionFactory.MSSQL) {
      try {
          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
          conn= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433","novel","jsp"); 
      }catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe);
      }      
    }


    else if (dbms == ConnectionFactory.MYSQL) {
     try {
   Class.forName("org.gjt.mm.mysql.Driver");
   conn = DriverManager.getConnection("jdbc:mysql://"+url+":"+port+"/"+dbname+"?user="+dbuser+"&password="+pswd);
      //+"&useUnicode="+unicode+"&characterEncoding="+encode
      } catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe);
      }      
    }

    return conn;
  }
}
---------------------------------------------------------------------------------

  public static final int ODBC = 1;
  public static final int ORACLE = 2;
  public static final int MSSQL= 3;
  public static final int MYSQL = 4;

 

4종류의 db일때...

필자는 mysql을 사용하고 로컬호스트에 jspdb를 만들었고

user 아이디는 jspuser...비번은 1234라면

  private String url = "localhost" ;
  private String port = "3306";
  private String dbname = "jspdb";
  private String dbuser = "jspuser" ;
  private String pswd = "1234";

로 써주면 되겠다.


    else if (dbms == ConnectionFactory.MYSQL) {
     try {
   Class.forName("org.gjt.mm.mysql.Driver");
   conn = DriverManager.getConnection("jdbc:mysql://"+url+":"+port+"/"+dbname+"?user="+dbuser+"&password="+pswd);
      //+"&useUnicode="+unicode+"&characterEncoding="+encode
      } catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe);
      }  


mysql일때 걸리는 부분 중간에 앞에서 지정해놓은 아이디 비전 디비주소가 자동으로 들어가게된다.


    return conn;


결과를 반환해주면서 끝




출처 : http://blog.naver.com/midniteblaze/10016476633