본문 바로가기
개발 수업/DB

이클립스에서 오라클 데이터베이스 연동

by 오늘 하루s 2023. 6. 29.
728x90
더보기

Day43. 230629

1. 이클립스에 'ojdbc6.jar' 라이브러리 추가하기

 

2. jsp파일 생성 후

오라클 데이터베이스 연결 코드 입력하기

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>부서목록</title>
</head>
<body>
<%--1.드라이브로딩
    2.커넥션얻기	
    3.객체준비
    4.쿼리실행
    5.자원반환
 --%>
 	<h2>부서목록(p380참고)</h2>
	<table border="1">
	 <thread>
	 <tr>
	  	<th>번호</th>
	  	<th>부서이름</th>
	  	<th>위치</th>
	  </tr>	
  </thead>
  <tbody> 

 <%
//http://localhost:8081/jp/ch14/viewMemberList_p380.jsp?memberid=hongid
 //클라이언트가 요청시 보낸 data를 받는다.
String memberid = request.getParameter("memberid");
 
 //1.드라이브로딩
	try {
		Class.forName("oracle.jdbc.OracleDriver"); //오라클용
		//Class.forName("com.mysql.cj.jdbc.Driver"); //My-SQL용
		System.out.println("1.JDBC Driver등록");
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
 
 //2.연결 커넥션얻기
 		//오라클용	 	
 		String url ="jdbc:oracle:thin:@172.30.1.86:1521/xe";
		String user = "scott";
		String password = "tiger";
 	 	
		//My-SQL용(p380 26~29라인)
		//String url ="jdbc:mysql://172.30.1.86:3306/chap14?useUnicode=true&characterEncoding=utf8";
		//String user = "jspexam";
		//String password = "jsppw";
		Connection conn = null;
		//Statement stmt = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = DriverManager.getConnection(url, user, password);
			System.out.println("2.연결 Connection얻기-성공");
		} catch (SQLException e) {
			e.printStackTrace();
		}
 
 //3.객체준비
   	try {
   		String sql = "select * from dept";
		/*String sql = "select no,memberid,password,name,email "+
   								"from member "+
								"where memberid=?";*/
			//stmt = conn.createStatement();
			stmt = conn.prepareStatement(sql);
		} catch (SQLException e1) {
			System.out.println("객체관련 에러발생="+e1);
			e1.printStackTrace();
		}
 
 //4.쿼리실행
try {
		//rs = stmt.executeQuery(sql);
		//stmt.setString(1,memberid);
		rs = stmt.executeQuery();
		
		System.out.println("번호\t부서명\t위치");
		System.out.println("---------------------------------------------------");
		while(rs.next()) {
			//컬럼 별칭사용시
							int deptno = rs.getInt("deptno"); //첫번째 컬럼
							String dname= rs.getString("dname");
							String loc = rs.getString("loc");
							System.out.printf("%8d %20s %10s \r\n", deptno,dname,loc);
						
 %>
 
		<%--회원수만큼 tr요소가 반복=>반복문사용--%>
		<%--반복문시작 --%>
		<tr>
		<td><%=deptno %></td>
		<td><%=dname %></td>
		<td><%=loc %></td>
		</tr>
		<%--반복문끝 --%>
		<%
		}//while

		} catch (SQLException e1) {
		System.out.println("executeUpdate()실행관련 에러발생");
		e1.printStackTrace();
		}

		//5.자원반환
		try {
					if(rs!=null) {rs.close();}
					if(stmt!=null) {stmt.close();}
					if(conn!=null) {conn.close();}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				System.out.println("5.자원반납");
		 %>
	</tbody>
	</table>
</body>
</html>

 

콘솔창

 
연결이 잘 되었다면 콘솔창과 웹페이지에 오라클 데이터베이스가 출력된다!

728x90