티스토리 뷰
1. JDBC란?
자바 프로그램 내에서 데이터베이스 질의문 즉, SQL을 실행하기 위한 자바 API(application programming interface)이다. Java database connectivity의 약자로 생각하기도 하지만 실제로는 상표 이름이다
interface에 장점을 이용한 대표적인 기술 중 하나. 사실 기존에는 database에 따라 프로그램 코드가 종속되었다. 이 말은 사용하던 database의 종류가 바뀐다면 이미 개발한 전체 프로그램 소스코드를 새로운 database에 맞게 고쳐야 된다는 불편함이 있었다.
이러한 점을 해결하기 위한 API이다.
자바언어에서 데이터베이스 표준 인터페이스를 정의하고, 각 데이터베이스 회사들은 JDBC 인터페이스를 제공받아 자신들의 데이터베이스에 맞게 구현한후 드라이버를 제공한다. 개발자가 JDBC API를 사용할 경우 DBMS에 알맞은 JDBC드라이버만 있으면 어떤 데이터베이스라도 사용할 수 있게 된다.
JDBC 드라이버는 각 밴드사에서 jar파일로 제공된다.
ORACLE에 경우
버젼마다 다른 jar파일 사용
9.2 classes12.jar 사용
* 라이브러리 추가 방법(eclipse mars)
1. 프로젝트명 우클릭
2. Properties 선택
3. Java Build Path 선택
4. Libraries 탭 Add External JARs..
5. 오라클 설치 디렉토리에 classes12.jar 추가
2. JDBC 코딩 순서
1) JDBC 드라이버 로딩
ex code)
Class.forName("oracle.jdbc.driver.OracleDriver");
2) 데이터베이스 connection 연결, 커넥션 객체 생성
ex code)
conn = DriverManager.getConnection("jdbc:oracle:thin:@190.200.10.103:1521:ORAGW","ORGDBADM","ORGDBADM");
3) 쿼리(sql)문장을 실행하기 위한 Statement / PreparedStatement / CallableStatement 객체 생성
ex code)
pstmt = conn.prepareStatement(lsql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
4) 쿼리 실행
ex code)
rs = pstmt.executeQuery();
5) 쿼리 실행의 결과값(int, ResultSet) 사용
ex code)
client.setDocid (rs.getString(1));
client.setSeq (rs.getString(2));
6) 사용된 객체 종료(ResultSet, Statement / PreparedStatement / CallableStatement , ex code)
객체변수.close() 사용
3. 예제소스 벡터형 변수를 리턴하는 파일 조회 코드
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 |
public static Vector listFile(String docid) {
Vector vect = new Vector();
String lsql = "";
int itotalrow = 0;
String ssqlerrm ="";
try {
lsql = "select DOC_ID , \n"
+ " SEQ , \n"
+ " P_DOC_ID, \n"
+ " CONTENT, \n"
+ " ORGFILENM, \n"
+ " MODFILENM, \n"
+ " SAVEPATH, \n"
+ " FILESIZE, \n"
+ " FILEEXT, \n"
+ " EDITID, \n"
+ " EDITDATE, \n"
+ " DELDATE, \n"
+ " EDITIP \n"
+ " from GW_USER.TGW_WEBHARD_ATTACH \n"
+ " where EDITID = ? \n"
+ " order by DOC_ID \n";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@IP:DB","ID","PASSWORD");
//stmt = conn.createStatement();
pstmt = conn.prepareStatement(lsql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
pstmt.setString(1, docid);
//stmt.executeUpdate(lsql);
//rs = stmt.executeQuery(lsql);
rs = pstmt.executeQuery();
rs.last();
itotalrow = rs.getRow();
FilePrm info = new FilePrm();
info.setIsqlflag(0);
info.setIsqlrow(itotalrow);
info.setSsqlerrm(ssqlerrm);
rs.beforeFirst();
while( rs.next() ) {
FilePrm client= new FilePrm();
client.setDocid (rs.getString(1));
client.setSeq (rs.getString(2));
client.setPdocid (rs.getString(3));
client.setContent (rs.getString(4));
client.setOrgfilenm (rs.getString(5));
client.setModfilenm (rs.getString(6));
client.setSavepath (rs.getString(7));
client.setFilesize (rs.getString(8));
client.setFileext (rs.getString(9));
client.setEditid (rs.getString(10));
client.setEditdate (rs.getString(11));
client.setDeldate (rs.getString(12));
client.setEditip (rs.getString(13));
//System.out.println("이름 : " + rs.getString("EDITID")+" "+rs.getString("ORGFILENM")+" "+rs.getString("MODFILENM"));
vect.addElement(client);
}
}
// SQLException
catch (SQLException e) {
System.out.println("SQLException in CnFilemtEnt.listFile(S) 에러 에러1 : [" + e.toString() + "]");
}
// Other Exceptions
catch (Exception e) {
System.out.println("Exception in CnFilemtEnt.listFile(S) 에러에러3 : [" + e.toString() + "]");
}
finally {
if(rs != null) try { rs.close() ; } catch (final Exception ex) {}
if(stmt != null) try { stmt.close(); } catch (final Exception ex) {}
if(conn != null) try { conn.close() ; } catch (final Exception ex) {}
if(pstmt != null) try { pstmt.close() ; } catch (final Exception ex) {}
return vect;
}
|
cs |
'개발 > JAVA' 카테고리의 다른 글
[Java 개념]3. 데이터 타입, 리터럴, 연산자 (0) | 2017.01.07 |
---|---|
[Java] Eclipse 단축키 (0) | 2017.01.07 |
[Java 개념]1.자바란? (0) | 2017.01.07 |
Camel rule (0) | 2017.01.01 |
상속 (2) | 2016.12.26 |
- Total
- Today
- Yesterday
- 장고앱
- 딥러닝 GPU #pytorch gpu
- SET CHAINED OFF
- 한림대의료원#친환경캠페인#감탄캠페인#종이 절약#에코한림
- removeNode
- removeNode()
- 파이토치 gpu 사용량
- 안드로이드 사인키
- 안드로이드 키
- 딥러닝 gpu 사용량
- pytorch GPU
- 파이토치
- 노드삭제
- 이미지 전처리
- django 웹서버
- unchained transaction mode
- gpu사용량
- Java
- Django
- 장고프로젝트
- 이미지 배경제거
- 생활속탄소저감 #감탄캠페인 #나는 오늘도 감탄을 했다 #에코한림
- Android Studio 키
- Grabcut
- django app
- 전경추출
- 딥러닝 gpu 설정
- 엘리먼트삭제
- 이미지전처리
- sp_procxmode#
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |