고인돌 프로젝트/[Step 1] 모델 1 , Jsp,beans + Oracle

5. Jsp 부분 작업 (with DB 연결)

고인돌개발자 2021. 8. 5. 14:43

1. jdbc 테스트 

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.sql.*" %>

<%
	String driver = "oracle.jdbc.driver.OracleDriver";
	String dbURL="jdbc:oracle:thin:@localhost:1521:xe";
	String user_id="scott";        
	String user_pw="tiger";
	String qry="";

	Connection conn = null; // DB 에 connection 된 객체를 저장 
    PreparedStatement ps = null;  // connection 객체에 실행문을 던지는 역할(창구)
    ResultSet rs = null;     // select 결과값을 받아옮

    try {
   	    
		/* Driver Loading */
        Class.forName(driver);
	    conn = DriverManager.getConnection(dbURL, user_id, user_pw);
    	
	    conn.setAutoCommit(false); // 자동커밋 해제
	    
	    System.out.println("Connection Success");
		
        /* Result Set , Print */	
		qry = " select id, name, age, to_char(reg_date,'yyyy.mm.dd') as dati " 
			+" from people ";
		
		ps = conn.prepareStatement(qry);
		rs = ps.executeQuery();
		
		while(rs.next()) {
			out.println("ID : "+rs.getString("id"));
			out.println("<br>");
			out.println("Name : "+rs.getString("name"));
			out.println("<br>");
			out.println("age : "+rs.getString("age"));
			
		}			
				
		
     }catch (Exception e) {
		System.out.println("Error =>"+e);
		conn.rollback();
	 }finally {
		 /* Close */ 
		try {
			if(rs != null) rs.close();
			if(ps != null) ps.close();
			if(conn != null) conn.close();			
		}catch (Exception e2) {			
		}
	}	

		

%>

2. /Model1/WebContent/jsp  > list.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.sql.*" %>
<jsp:useBean id="dbConn" class="comm.DbConn" scope="page" />


<%
	Connection conn = null; // DB 에 connection 된 객체를 저장 
    PreparedStatement ps = null;  // connection 객체에 실행문을 던지는 역할(창구)
    ResultSet rs = null;     // select 결과값을 받아옮
    String qry="";
    
    conn = dbConn.getConn();
    	
 %>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Select </title>
</head>
<body>


<h1> ■모델 1 </h1>

 <a href="/index.html">● Home </a>
 <p>
 
 <table style="width: 400px;" border="0">
		<tr style="height: 40px">
			<td>ID</td>
			<td align="center">이름</td>
			<td align="center">나이</td>
			<td align="center">등록일자</td>			
			<td> 삭제 </td>
		</tr>
		
	<%
	
	  try{
		  	
		  	
		    String strId="";
			 /* Result Set , Print */	
			qry = " select id, name, age, to_char(reg_date,'yyyy.mm.dd') as dati " 
				+" from people ";
			
			ps = conn.prepareStatement(qry);
			rs = ps.executeQuery();
			
			while(rs.next()) {
				strId = rs.getString("id");
			%>	
				<tr style="height: 40px">
				<td><%=strId%></td>
				<td align="center"><%=rs.getString("name")%></td>
				<td align="center"><%=rs.getString("age")%></td>
				<td align="center"><%=rs.getString("dati")%></td>
				<td><a href="/jsp/delete.jsp?id=<%=strId%>">삭제 </a></td>
			</tr>	
			<%
			}		
	  }catch (Exception e) {
			System.out.println("Error =>"+e);			
		 }finally {
			 /* Close */ 
			try {
				if(rs != null) rs.close();
				if(ps != null) ps.close();
				if(conn != null) conn.close();			
			}catch (Exception e2) {			
			}
		}	
		
	%>	
		
		
	</table>

</body>
</html>

3. /Model1/WebContent/jsp  > regist_proc.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.sql.*" %>
<jsp:useBean id="dbConn" class="comm.DbConn" scope="page" />

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Delete</title>
</head>
<body>


<h1> ■모델 1 </h1>

 <a href="/index.html">● Home </a>
 <p>
 
<%

		Connection conn = null; // DB 에 connection 된 객체를 저장 
		PreparedStatement ps = null;  // connection 객체에 실행문을 던지는 역할(창구)
		ResultSet rs = null;     // select 결과값을 받아옮
		String qry="";
		
		conn = dbConn.getConn();
    
    try{
    		request.setCharacterEncoding("UTF-8");
    	
		    String strID 	= request.getParameter("input_id");
		    String strName 	= request.getParameter("input_name");
		    String strAge	= request.getParameter("input_age");
		    
		     /* Result Set , Print */	
			qry = " Insert into people(id, name, age) "
				 +" values (? , ?, to_number(?))";
		     
			ps = conn.prepareStatement(qry);			
			ps.setString(1, strID);
			ps.setString(2, strName);
			ps.setString(3, strAge);			
			
			ps.executeUpdate();
			
			out.println("ID "+strID+" 를 등록 했습니다.");
			
	  }catch (Exception e) {
			out.println("Error => 오류가 발생했습니다. "+e);			
		 }finally {
			 /* Close */ 
			try {
				if(rs != null) rs.close();
				if(ps != null) ps.close();
				if(conn != null) conn.close();			
			}catch (Exception e2) {			
			}
		}	
		
	%>		

</body>
</html>

4. /Model1/WebContent/jsp  > delete.jsp

더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="java.sql.*" %>
<jsp:useBean id="dbConn" class="comm.DbConn" scope="page" />

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Delete</title>
</head>
<body>


<h1> ■모델 1 </h1>

 <a href="/index.html">● Home </a>
 <p>
 
<%
		Connection conn = null; // DB 에 connection 된 객체를 저장 
		PreparedStatement ps = null;  // connection 객체에 실행문을 던지는 역할(창구)
		ResultSet rs = null;     // select 결과값을 받아옮
		String qry="";
		
		conn = dbConn.getConn();
    
    try{
		    String strID = request.getParameter("id");
		    
		     /* Result Set , Print */	
			qry = " delete from people where id=?";
			 
			ps = conn.prepareStatement(qry);			
			ps.setString(1, strID);
			
			ps.executeUpdate();
			
			out.println("ID "+strID+" 를 삭제했습니다.");
			
	  }catch (Exception e) {
			out.println("Error => 오류가 발생했습니다. "+e);			
		 }finally {
			 /* Close */ 
			try {
				if(rs != null) rs.close();
				if(ps != null) ps.close();
				if(conn != null) conn.close();			
			}catch (Exception e2) {			
			}
		}	
		
	%>		

</body>
</html>

▶ 프로그램 테스트 

'고인돌 프로젝트 > [Step 1] 모델 1 , Jsp,beans + Oracle' 카테고리의 다른 글

7. [번외] Was (tomcat) 배포  (0) 2021.08.05
6. DB 공통 Bean 분리  (0) 2021.08.05
4. DB(Oracle) 작업  (0) 2021.08.05
3. 기본 Html 작성  (0) 2021.08.05
2. 프로젝트 생성  (0) 2021.08.05