Database(2023-03-29)

권단비·2023년 3월 29일
0

IT

목록 보기
110/139
Class.forName("oracle.jdbc.driver.OracleDriver");
⇒오라클의 jdbc를 이클립스의 lib 폴더에 복사

[파일 경로]
C:\Users\USER\Documents\oracle\sqldeveloper\jdbc\lib
・Connection Ctrl+Space : sql로 import

[오라클 연동 시 필요 코드]
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "scott", "tiger");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);

Connection : 빨대 연결 | 인터페이스 형식. 자손이 구현해라 (오라클 등의 jdbc driver 연결)
Statement : 쿼리를 실행할 수 있는 객체
ResultSet : 결과물을 받아오는 셋

[Oracle과 Eclipse 연동]

[계산]
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		String url = "jdbc:oracle:thin:@localhost:1521/xe"; // 1521 : 오라클 포트 번호 || 무조건 복사하여 사용
		Class.forName("oracle.jdbc.driver.OracleDriver");
		// new oracle.jdbc.driver.OracleDriver(); 객체생성과 동일한 역할
		// jdbc driver를 올리는 작업

		String sql = "SELECT * FROM dept";

		// Connection Ctrl+Space : sql로 import
		Connection con = DriverManager.getConnection(url, "scott", "tiger");
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(sql);

		out.print("<table border='1'>");
		out.print("<th>" + "DEPTNO" + "</th>");
		out.print("<th>" + "DNAME" + "</th>");
		out.print("<th>" + "LOC" + "</th>");

		while (rs.next()) {
			out.print("<tr>");
			out.print("<td>" + rs.getString("deptno") + "</td>");
			out.print("<td>" + rs.getString("dname") + "</td>");
			out.print("<td>" + rs.getString("loc") + "</td>");
			out.print("</tr>");
		}
		out.print("</table>");

		// 		while (rs.next()) {
		// 			out.print("부서번호 : " + rs.getString("deptno") + "</br>");
		// 			out.print("부서명 : " + rs.getString("dname") + "</br>");
		// 			out.print("근무지 : " + rs.getString("loc") + "<hr>");
		// 		}

		rs.close();
		st.close();
		con.close();
	%>
</body>
</html>
[결과값]


[계산]
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		String url = "jdbc:oracle:thin:@localhost:1521/xe"; // 1521 : 오라클 포트 번호 || 무조건 복사하여 사용
		Class.forName("oracle.jdbc.driver.OracleDriver");
		// new oracle.jdbc.driver.OracleDriver(); 객체생성과 동일한 역할
		// jdbc driver를 올리는 작업

		String sql = "SELECT * FROM emp";

		// Connection Ctrl+Space : sql로 import
		Connection con = DriverManager.getConnection(url, "scott", "tiger");
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(sql);

		out.print("<table border='1'>");
		out.print("<th>" + "EMPNO" + "</th>");
		out.print("<th>" + "ENAME" + "</th>");
		out.print("<th>" + "JOB" + "</th>");
		out.print("<th>" + "MGR" + "</th>");
		out.print("<th>" + "HREDATE" + "</th>");
		out.print("<th>" + "SAL" + "</th>");
		out.print("<th>" + "COMM" + "</th>");
		out.print("<th>" + "DEPTNO" + "</th>");
		while (rs.next()) {
			out.print("<tr>");
			out.print("<td>" + rs.getString("EMPNO") + "</td>");
			out.print("<td>" + rs.getString("ENAME") + "</td>");
			out.print("<td>" + rs.getString("JOB") + "</td>");
			out.print("<td>" + rs.getString("MGR") + "</td>");
			out.print("<td>" + rs.getString("HIREDATE") + "</td>");
			out.print("<td>" + rs.getString("SAL") + "</td>");
			out.print("<td>" + rs.getString("COMM") + "</td>");
			out.print("<td>" + rs.getString("DEPTNO") + "</td>");
			out.print("</tr>");
		}
		out.print("</table>");

		rs.close();
		st.close();
		con.close();
	%>
</body>
</html>
[결과값]


[계산]
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<!-- ---각 사원의 급여가 몇 등급인지 salgrade.jsp -->
	<%
		String url = "jdbc:oracle:thin:@localhost:1521/xe"; // 1521 : 오라클 포트 번호 || 무조건 복사하여 사용
		Class.forName("oracle.jdbc.driver.OracleDriver");
		// new oracle.jdbc.driver.OracleDriver(); 객체생성과 동일한 역할
		// jdbc driver를 올리는 작업

		String sql = "SELECT * FROM emp, salgrade WHERE sal BETWEEN losal AND hisal";

		// Connection Ctrl+Space : sql로 import
		Connection con = DriverManager.getConnection(url, "scott", "tiger");
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(sql);

		out.print("<table border='1'>");
		out.print("<th>" + "ENAME" + "</th>");
		out.print("<th>" + "SAL" + "</th>");
		out.print("<th>" + "GRADE" + "</th>");
		while (rs.next()) {
			out.print("<tr>");
			out.print("<td>" + rs.getString("ENAME") + "</td>");
			out.print("<td>" + rs.getString("SAL") + "</td>");
			out.print("<td>" + rs.getString("Grade") + "</td>");
			out.print("</tr>");
		}
		out.print("</table>");

		rs.close();
		st.close();
		con.close();
	%>
</body>
</html>
[결과값]


0개의 댓글