티스토리 뷰

Programming/Java

JDBC 연결

파란크리스마스 2012. 6. 24. 10:08
728x90

MySQL JDBC 연결

import java.sql.Connection;
import java.sql.DriverManager;

public class Test {
  public static void main(String[] args) throws Exception {
    Connection conn = null;
    try {
      Class.forName("org.gjt.mm.mysql.Driver"); 
      conn = DriverManager.getConnection("jdbc:mysql://localhost:5515/dbname?user=id&useUnicode=true&characterEncoding=UTF8", "id", "pw");
    } catch (Exception e) {
      e.printStackTrace();
      if (conn!=null) try { conn.close(); } catch (Exception e2) { }
    }
  }
}

Oracle thin

import java.sql.Connection;
import java.sql.DriverManager;

public class Test {
  public static void main(String[] args) throws Exception {
    Connection conn = null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
      
      System.out.println(conn);
      
    } catch (Exception e) {
      e.printStackTrace();
      if (conn!=null) try { conn.close(); } catch (Exception e2) { }
    }
  }
}

Oracle OCI

import java.sql.Connection;
import java.sql.DriverManager;

public class TestOCI {
  public static void main(String[] args) throws Exception {
    Connection conn = null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      conn = DriverManager.getConnection("jdbc:oracle:oci:@xe", "scott", "tiger");
      
      System.out.println(conn);
      
    } catch (Exception e) {
      e.printStackTrace();
      if (conn!=null) try { conn.close(); } catch (Exception e2) { }
    }
  }
}
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
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
글 보관함