본문 바로가기

Study/Programming

JSP DB연동

반응형
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

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

<%
    Connection conn = null;
    Statement stmt = null;
    StringBuffer sb = null;
    ResultSet rs = null;
   
    try {
        String strUrl = "jdbc:oracle:thin:@211.183.2.35:1521:orcl";
        String strId = "scott";
        String strPwd = "tiger";
       
        Class.forName("oracle.jdbc.driver.OracleDriver");
       
        conn = DriverManager.getConnection(strUrl, strId, strPwd);
       
        stmt = conn.createStatement();
        String query ="select * from dept";
        rs = stmt.executeQuery(query);
       
        sb = new StringBuffer();
        sb.append("<table border='1'>");
        while(rs.next()){
            sb.append("<tr>");
            sb.append("<td>" +rs.getInt("deptno") +"</td>");
            sb.append("<td>" +rs.getString("dname") +"</td>");
            sb.append("<td>" +rs.getString("loc") +"</td>");
            sb.append("</tr>");
        }
        sb.append("</table>");
                   
    } catch (ClassNotFoundException e) {
        System.out.println("드라이버 로드 실패");
    } catch (SQLException e){
        System.out.println("데이터베이스 연결 실패" +e.toString());
    } finally{
        if(stmt != null) try{ conn.close(); } catch(SQLException e) {}
        if(conn != null) try{ conn.close(); } catch(SQLException e) {}
    }

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= sb %>
</body>
</html>
반응형

'Study > Programming' 카테고리의 다른 글

JSP 서버이름, 클라이언트ip  (0) 2009.05.28
jsp 달력, 구구단  (0) 2009.05.27
JSP 날짜 구하기  (0) 2009.05.27
JSP 구구단  (0) 2009.05.27
자바 TCP MultiChat  (0) 2009.05.26