본문 바로가기

JAVA/JSP & Servlet

JSP 기본 실습 : ==데이터베이스 연결 (JDBC)==

이클립스에서 JSP용 JDBC 설정

 

WebContent -> lib 폴더 안에 ojdbc6.jar 파일을 복사해서 붙여넣음

 

 

 

 


 

 

<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.Statement"%>

<%@page import="java.sql.Connection"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSP : jdbc</title>

</head>

<body>

 

<%

        Connection conn =null;

        Statement stmt = null;

        ResultSet rs = null;

       

        Class.forName("oracle.jdbc.driver.OracleDriver");

        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");

       

        String query = "select * from emp";

        stmt = conn.createStatement();

        rs = stmt.executeQuery(query);

       

        while(rs.next()){

               out.println(rs.getString("empno"));

        }

       

%>

 

 

</body>

</html>