이 포스트에서는 jsp로 데이터를 수정하여 mysql의 테이블에 있는 데이터를 수정합니다.


* modify_form.jsp



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Read Database</title>
</head>
<body>
<%@ page import = "java.sql.*, java.util.*" %>
MySQL 데이터를 읽고 수정하기
<form name="form" method="post" action="jsp_edit.jsp">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from jsp");
%>
<%while(rs.next()) {%>
<br><br> ID: <input type="text" name="id" value="<%= rs.getString("id") %>">
<br><br>Password: <input type="text" name="password" value="<%=rs.getString("password") %>">
<%
}
} catch (SQLException e) { %>
SQL 예외 
<%
} finally {
if(rs!= null)
try {
rs.close();
} catch(SQLException e) {}
if(stmt!= null)
try {
stmt.close();
} catch(SQLException e) {}
if(conn!= null)
try {
conn.close();
} catch(SQLException e) {}
}
%>
<input type="submit" name="formbutton1" value="수정하기">
</form>
</body>
</html> 



* modify_DB.jsp




 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Update Datebase</title>
</head>
<body>
<%@ page import= "java.sql.*, java.util.*" %>
MySQL 데이터 수정하기
<%
request.setCharacterEncoding("UTF-8");

String ID = request.getParameter("id");
String PW = request.getParameter("password");

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
int updateCount = 0;

try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
stmt = conn.createStatement();
updateCount = stmt.executeUpdate("update jsp set password = '"+PW+"' where id='"+ID+"'");
%>
<%} finally {
if(stmt != null)
try{
stmt.close();
} catch (Exception e) {}
if(rs != null)
try {
rs.close();
} catch (Exception e) {}
if(conn != null)
try {
conn.close();
} catch (Exception e) {}
}
%>
<% if(updateCount > 0) { %>
수정되었습니다.
<%} else { %>
동일한 아이디가 존재하지 않습니다.
<% } %>
</body>
</html>

좋은 글이면 손가락 눌러 주세요. 힘이 됩니다 ^^ 

로그인이 필요 없습니다.



* modify_form.jsp 실행 결과





수정하기버튼 클릭 화면
  두 번째에 있는 데이터 afreeca1122, 1122 의 정보의 비밀번호를 0101 로 수정하겠습니다.







* read_DB.jsp 실행 화면
 변경된 데이터를 확인하기 위해 read_DB.jsp 를 실행합니다.







posted by 쪼재