Q1. 오라클 DB연동
DB연동을 위한 커넥션 풀을 만든다
[ 게시판 구성 파일 ]
글쓰기에 필요한 1~5까지 사용
0. 사전 설정
- server - context.xml
<Resource name="jdbc/basicjsp"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
username="system" // 오라클 ID
password="1234" // 오라클 password
url="jdbc:oracle:thin:@//localhost:1521/xe"
maxWait="5000"
/>
- WEB-INF - web.xml ( 아래부분 디폴트부터 변경)
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>basicjsp db</description>
<res-ref-name>jdbc/basicjsp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
DB연동 시 필요한 항목을 "jdbc/basicjsp" 로 찾아서 사용한다
1. 오라클과 연동로직
- DataBean 생성 (setter,getter)
- DBBean 생성 (커넥션풀, Context, DataSource 프리셋 복붙)
private static PersonDBBean instance = new PersonDBBean();
//.jsp 페이지에서 DB연동빈인 PersonDBBean 연동 시 필요 // 싱글톤
public static Perso2DBBean getInstance() {
return instance;
}
//커넥션풀에서 Connection 객체를 얻어냄
private Connection getConnection() throws Exception {
Connection conn=null;
Context ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/basicjsp");
conn = ds.getConnection();
return conn;
}
lookup으로 jdbc/basicjsp를 찾는 이유는
context.xml, web.xml에 DB연동 시 필요한 항목을 "jdbc/basicjsp" 로 정의해두었다 (0번 참조)
2. 빈에서 호출 메소드 예시 [ Person2DBBean.java ]
while(rs.next()) 안의 내용 구성
1. 데이터 빈 객체를 생성
2. rs객체로 부터 읽어와서 빈 객체에 데이터를 설정... (ex : setXXX(rs.getXXX("필드명"))
3. 리스트 추가
//list만들어서 출력하기
public List<PersonDataBean> listPerson() {
List<PersonDataBean> list = new ArrayList<PersonDataBean>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from person2";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
// executeQuery 이클립스에 명령을 보냄
// next() 다음값이 있으면 실행
while(rs.next()) {
PersonDataBean p = new PersonDataBean();
p.setName(rs.getString("name"));
String[] idnumArray = rs.getString("idnum").split("-");
// String[] 배열, split 문자나누기 아랫문 참조
p.setIdnum1(idnumArray[0]);
p.setIdnum2(idnumArray[1].substring(0,1)+"******");
// substring 0~1자리 추출 + * 표시 (주민번호 뒷자리 가림)
list.add(p);
}
} catch (Exception ex) {
ex.printStackTrace();
}finally {
if(rs!=null)try {rs.close();}catch(SQLException ex) {}
if(pstmt!=null)try {pstmt.clearParameters();}catch(SQLException ex) {}
if(conn!=null)try {conn.close();}catch(SQLException ex) {}
}
return list;
}
'DEV Heart' 카테고리의 다른 글
| [ MVC 1 게시판 ] #2 리스트 출력하기 (List) (0) | 2021.09.08 |
|---|---|
| [ MVC 1 게시판 ] #2 글쓰기 (WriteForm) (0) | 2021.09.08 |
| [ HashMap ] #3 HashMap으로 2개 Table 출력하기 (Inner Join) (0) | 2021.09.01 |
| [ HashMap ] #2 HashMap으로 Sql table 출력하기 (0) | 2021.08.31 |
| [ HashMap ] #1 <Key, Value> put, get 사용법 (0) | 2021.08.31 |