<%@ page contentType="text/html;charset=EUC-KR" import="java.sql.*" %> <% Class.forName("org.gjt.mm.mysql.Driver"); Connection conn = null; Statement stmt = null; ResultSet rs = null; String id = "", passwd = "", name = "", mem_num1 = "", mem_num2 = "", e_mail = "", phone = "", zipcode = "", address = "", job = ""; int counter = 0; try{ conn = DriverManager.getConnection("jdbc:mysql://cse-stu.hansung.ac.kr:3306/COM0026?useUnicode=true&characterEncoding=euc-kr", "sklee", "sklee123"); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM userdb"); %> JSP에서 데이터베이스 연동

JSP 스크립틀릿에서 데이터베이스 연동 예제입니다



회원정보

<% if(rs!=null){ while(rs.next()){ id = rs.getString("id"); passwd = rs.getString("passwd"); name = rs.getString("name"); mem_num1 = rs.getString("mem_num1"); mem_num2 = rs.getString("mem_num2"); e_mail = rs.getString("e_mail"); phone = rs.getString("phone"); zipcode = rs.getString("zipcode"); address = rs.getString("address"); job = rs.getString("job"); %> <% counter++; } } %>
ID PASSWD NAME MEM_NUM1 MEM_NUM2 E_MAIL PHONE ZIPCODE/ADDRESS JOB
<%= id %> <%= passwd %> <%= name %> <%= mem_num1 %> <%= mem_num2 %> <%= e_mail %> <%= phone %> <%= zipcode %>/<%= address %> <%= job %>
total records : <%= counter %> <% }catch(SQLException sqlException){ out.println("sql exception = " + sqlException); }catch(Exception exception){ System.out.println("exception"); }finally{ if( rs != null ) try{ rs.close(); } catch(SQLException ex) {} if( stmt != null ) try { stmt.close(); } catch(SQLException ex) {} if( conn != null ) try{ conn.close(); } catch(Exception ex){} } %>