에러 탈출 일지

Delete Test 01 본문

개발/JavaScript

Delete Test 01

뉴NEW 2022. 10. 25. 23:23

Delete Test 01 Form

<style>
#container {
	width: 500px;
	margin: 0 auto;
}

h2 {
	text-align: center;
}

#deleteForm {
	border: 1px solid black;
	padding: 10px;
}

.d_input {
	width: 100%;
	margin: 10px;
	padding: 10px;
}

.d_input label {
	display: inline-block;
	width: 100px;
	margin-left: 60px;
	font-size: 1.1em;
	font-weight: bold;
}

.d_input input[type="text"] {
	margin-left: 20px;
	height: 23px;
}

.d_btns {
	text-align: center;
	margin-top: 30px 0 10px;
}

.d_btns input {
	width: 100px;
	height: 30px;
	background-color: black;
	color: white;
	font-size: 0.9em;
	font-weight: bold;
}
</style>
<script>
	document.addEventListener("DOMContentLoaded", function() {
		let btnDelete = document.getElementById("btnDelete");
		btnDelete.addEventListener("click", function() {
			let form = document.deleteForm;
			if (!form.id.value) {
				alert("아이디를 입력하시오.");
				form.id.focus();
				return;
			}
			if (!form.pwd.value) {
				alert("비밀번호를 입력하시오.");
				form.pwd.focus();
				return;
			}
			form.submit();
		})

	})
</script>
</head>
<body>
<div id="container">
    <h2>회원 정보 삭제 폼 페이지</h2>

    <form action="deleteTest01Pro.jsp" method="post" id="deleteForm" name="deleteForm">
        <div class="d_input">
            <label for="id">아이디</label><input type="text" id="id" name="id">
        </div>
        <div class="d_input">
            <label for="pwd">비밀번호</label><input type="text" id="pwd" name="pwd">
        </div>

        <div class="d_btns">
            <input type="button" value="입력완료" id="btnDelete">&ensp;&ensp;
            <input type="reset" value="삭제">
        </div>
    </form>
</div>
</body>

 

Delete Test 01 Pro

<body>
	<h2>회원 정보 삭제</h2>
	<p>delete test 01</p>
	<%
		request.setCharacterEncoding("utf-8");
	%>

	<jsp:useBean id="member" class="test.Member"></jsp:useBean>
	<jsp:setProperty property="*" name="member" />

	<%
		// parameter 정보
	String id = member.getId();
	String pwd = member.getPwd();
	String name = member.getName();
	int age = member.getAge();

	// DB 접속 정보
	String url = "jdbc:mysql://localhost:3306/test01?useSSL=false";
	String dbId = "aws01";
	String dbPwd = "1234";

	// JDBC 객체 - 연결, 질의
	Connection conn = null;
	//Statement stmt = null;
	PreparedStatement pstmt = null;

	// (1) SQL 문 - Statement insert문   
	//String sql = "delete from member where if='" + id + "' and pwd='" +pwd+"'"; 

	// (2) SQL문 - PreparedStatement insert문
	// ?: 바인딩 변수, 폴더
	String sql = "delete from member where id=? and pwd=?";

	int cnt = 0;

	try {
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection(url, dbId, dbPwd);

		// (1) Statement
		//stmt = conn.createStatement();
		//cnt = stmt.executeUpdate(sql);

		// (2) PreparedStatement
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, id);
		pstmt.setString(2, pwd);

		cnt = pstmt.executeUpdate();

		if (cnt > 0)
			out.print("데이터 삭제에 성공하였습니다.");
		else
			out.print("데이터 삭제에 실패하였습니다.");

	} catch (Exception e) {
		e.printStackTrace();
		out.print("데이터 삭제 시에 예외가 발생하였습니다.");
	} finally {
		//if(stmt != null) try {stmt.close();} catch(Exception e) {e.printStackTrace();}
		if (pstmt != null) try {pstmt.close();} catch (Exception e) {e.printStackTrace();}
		if (conn != null) try {conn.close();} catch (Exception e) {e.printStackTrace();}
	}
	%>
</body>

 

'개발 > JavaScript' 카테고리의 다른 글

select Test01  (0) 2022.10.25
[Javacript] jQuery  (0) 2022.09.28
[JavaScript] 키(Key) 이벤트  (0) 2022.09.26
[JavaScript] DOM 요소의 추가 (document 객체)  (0) 2022.09.21
[JavaScript] BOM 객체 (location, history)  (0) 2022.09.20