1. DB 접속
- Connection 클래스 -> java.sql.Connection
Connection conn = null;
2. 연결 문자열 생성
- 접속에 필요한 정보로 구성된 문자열, Connecting String
String url = "jdbc:oracle:thin:@localhost:1521:xe"; =>jdbc:oracle:드라이버:@호스트명(서버IP/도메인주소):포트번호:SID
String id = "hr "; => 아이디
String pw = "1234" => 패스워드
3. JDBC 드라이버 로딩
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
4.접속
- Connection 객체 생성 + 접속 작업
conn = DriverManager.getConnection(url, id, pw);
System.out.println(conn.isClosed()); =>접속중(false),접속종료(true)
5.SQL
6.접속종료(반드시 접속 종료해야됨)
conn.close();
System.out.println(conn.isClosed()); => 확인차원
} catch(Exception e) {
e.printStackTrace();
}
**jdbc접속메소드
public static Connection getConnection(String server, String id, String pw) { //프로젝트 용
Connection conn = null;
String url = "jdbc:oracle:thin:@"+ server +":1521:xe";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, id, pw);
return conn;
} catch (Exception e) {
System.out.println("DBUtil.getConnection() : " + e.toString());
}
return null;
}
'JDBC' 카테고리의 다른 글
JDBC(CallableStatement 클래스) 예제(위경도 거리) (0) | 2018.09.19 |
---|---|
JDBC(CallableStatement 클래스) (0) | 2018.09.18 |
JDBC(PreparedStatement 클래스) (0) | 2018.09.18 |
JDBC(Statement 예제) - 메모장 (0) | 2018.09.17 |
JDBC(Statement 클래스) (0) | 2018.09.17 |