상세 컨텐츠

본문 제목

mysql.jdbc 사용하기 예

JSP·자바·코틀린

by 김일국 2010. 9. 7. 16:31

본문

<%@ page contentType="text/html; charset=MS949" %>
<%@ page import="java.sql.*, java.io.*" %>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<html>
<head>
<title>
회원 목록
</title>
</head>
<body bgcolor="#ffffff">
MEMBER 테이블의 내용

<table width="100%" border="1">
 
  <tr>
    <td>값1</td><td>값2</td><td>값3</td><td>값4</td>
  </tr>

  <%
   //1. JDBC 드라이버 로딩
       Class.forName("com.mysql.jdbc.Driver");
       Connection con = null;
       Statement stmt = null;
       ResultSet rs = null;

       try {
         String query = " select  aid, spid, atitle, aregiste from article, section where (apublish='P01001' or apublish='P02001') and spid=sid and sgroup='3' order by aregiste desc, areg_time desc limit 1 ";
         //2.데이터 베이스 커넥션 생성
   con = DriverManager.getConnection("jdbc:mysql://디비IP:3306/DB명", "아이디", "암호");
         //3. Statement 생성
         stmt = con.createStatement();
         //4. 쿼리 실행
         rs = stmt.executeQuery(query);
         //5. 쿼리 실행 결과 출력
         while(rs.next()) {
  %>
 
  <tr>
    <td><%= rs.getString("AID") %></td>
    <td><%= rs.getString("SPID")%></td>
    <td><%= rs.getString("ATITLE") %></td>
 <td><%= rs.getString("AREGISTE") %></td>
  </tr>
 
  <%
         }
       }catch(SQLException ex) {
         //에러 발생
   System.out.println("프로그램 오류가 있습니다.");
   return;
       }finally {
         //6. 사용한 Statement 종료
         if(rs != null) try { rs.close();} catch(SQLException ex) {}
         if(stmt != null) try { stmt.close();} catch(SQLException ex) {}
       
         //7. 커넥션 종료
         if( con != null) try { con.close(); }catch(SQLException ex) {}
       }
  %>
 
</table>

</body>
</html>

관련글 더보기

댓글 영역