-
Eclipse OJDBC SettingJSP Servlet 2022. 7. 13. 15:03
1. Oracle 설치
2. Eclipse Oracle DB연동
3. 연동 확인
3-1. JSP파일 생성
3-2. DB연결 코드 입력
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.Connection"%> <%@page import="oracle.jdbc.driver.OracleDriver"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>Hello World</h1> <table border="1"> <tr> <th>사번</th> <th>first_name</th> <th>last_name</th> </tr> <% // 클래스 로드 Class.forName("oracle.jdbc.driver.OracleDriver"); // connection 취득 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe", "hr", "hr"); out.print(conn); // 문장 생성 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM EMPLOYEES ORDER BY EMPLOYEE_ID"); // 결과집합 생성 ResultSet rs = pstmt.executeQuery(); // 결과집합 순환 후 적절히 가공 while(rs.next()) { %> <tr> <td><%=rs.getInt("EMPLOYEE_ID")%></td> <td><%=rs.getString("FIRST_NAME")%></td> <td><%=rs.getString("LAST_NAME")%></td> </tr> <% } %> </table> </body> </html>
3-3. 실행 결과
4. Maven Project OJDBC 연동
4-1. MVNREPOSITORY
CTRL + C
pom.xml <dependencies> 안에 CTRL + V
4-1. DB연결 코드 입력
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.Connection"%> <%@page import="oracle.jdbc.driver.OracleDriver"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>Hello World</h1> <table border="1"> <tr> <th>사번</th> <th>first_name</th> <th>last_name</th> </tr> <% // 클래스 로드 Class.forName("oracle.jdbc.driver.OracleDriver"); // connection 취득 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xe", "hr", "hr"); out.print(conn); // 문장 생성 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM EMPLOYEES ORDER BY EMPLOYEE_ID"); // 결과집합 생성 ResultSet rs = pstmt.executeQuery(); // 결과집합 순환 후 적절히 가공 while(rs.next()) { %> <tr> <td><%=rs.getInt("EMPLOYEE_ID")%></td> <td><%=rs.getString("FIRST_NAME")%></td> <td><%=rs.getString("LAST_NAME")%></td> </tr> <% } %> </table> </body> </html>
'JSP Servlet' 카테고리의 다른 글
Eclipse MyBatis Setting (0) 2022.07.13 Eclipse Lombok Setting (0) 2022.07.13 Eclipse MAVEN Setting (0) 2022.07.12 Eclipse Tomcat9 Setting (0) 2022.07.12 JSP Servelet 이론(4) (0) 2022.06.02