MySQL/jsp example
MySQL jsp example
Soul-Learner
2015. 6. 9. 14:21
JSP 에서 MySQL 데이터베이스에 연결하고 데이터를 가져오는 예
<%@ pagecontentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ page import = "java.sql.*" %> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String url = "jdbc:mysql://localhost:3306/데이터베이스 이름"; String id = "계정 아이디"; String pw = "계정 비밀번호"; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(url,id,pw); String sql = "select * from board_qna"; pstmt = conn.prepareStatement( sql); rs = pstmt.executeQuery(); if(!rs.next()) { out.println("검색된 결과가 없습니다"); } int num = rs.getInt("NUM"); String wdate = rs.getString("DATE"); String title = rs.getString("TITLE"); out.println("연결에 성공했습니다"+"<br>"); out.println("글번호: "+num+"<br>"); out.println("날짜: "+wdate+"<br>"); out.println("제목: "+title+"<br>"); }catch(Exception e){ out.println("오류발생:"); e.printStackTrace(new java.io.PrintWriter(out)); }finally { if(rs!=null) rs.close(); if(pstmt!=null)pstmt.close(); if(conn!=null) conn.close(); } %>