본문 바로가기
Backend/JAVA

jdbc update 쿼리 실행

by YERIEL_염주둥 2020. 3. 26.
728x90

1. 불러온 리스트 수정하기

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%= request.getRequestURI() %> <br>
회원 리스트 <br>
<table width="100%" border="1">
<tr>
	<td>아이디</td><td>비번</td><td>권한</td><td>이름</td><td>이메일</td><td>수정하기</td>
</tr>

<%
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	Class.forName("com.mysql.jdbc.Driver");
	try{
		String jdbcDriver = "jdbc:mysql://localhost:3306/dev35db?" +
				"useUnicode=true&characterEncoding=euckr";
		String dbUser = "dev35id";
		String dbPass = "dev35pw";
		conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
		System.out.println(conn + "<-- conn m_list.jsp");
		if(conn != null){
			out.println("01 DB연결 성공");
		}else{
			out.println("02 DB연결 실패");
		}

		pstmt = conn.prepareStatement("select * from tb_member");

		rs = pstmt.executeQuery();
		System.out.println(rs + "<-- rs m_list.jsp");
		System.out.println(rs.next() + "<-- rs.next() m_list.jsp");
		//System.out.println(rs.next() + "<-- rs.next() m_list.jsp");

		while(rs.next()){
%>
		<tr>
			<td><%= rs.getString("m_id")%></td>
			<td><%= rs.getString("m_pw")%></td>
			<td><%= rs.getString("m_level")%></td>
			<td><%= rs.getString("m_name")%></td>
			<td><%= rs.getString("m_email")%></td>
			<td>
<a href="<%=request.getContextPath() %>/mupdate/m_update_form2.jsp?send_id=<%= rs.getString("m_id")%>">수정버튼</a>
			</td>
		</tr>
<%
		}

	} catch(SQLException ex) {
		out.println(ex.getMessage());
		ex.printStackTrace();
	} finally {
		if (rs != null) try { rs.close(); } catch(SQLException ex) {}
		if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
        
		if (conn != null) try { conn.close(); } catch(SQLException ex) {}
	}
%>	

</table>

 

2. 수정 form 만들기

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<%
//list에서 값 받아오고 확인하기
String send_id = request.getParameter("send_id");
System.out.println(send_id + "<--send_id2");

//밑에서 값을 뿌려주기 위해 변수 선언 
String ud_id = null;
String ud_pw = null;
String ud_level = null;
String ud_name = null;
String ud_email = null;

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

Class.forName("com.mysql.jdbc.Driver");
try{
	String jdbcDriver = "jdbc:mysql://localhost:3306/dev35db?" +
			"useUnicode=true&characterEncoding=euckr";
	String dbUser = "dev35id";
	String dbPass = "dev35pw";
	conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
	System.out.println(conn + "<-- conn2");
	
	//executeQuery : SELECT 구문을 수행할 때 사용되는 함수입니다.
	pstmt = conn.prepareStatement("SELECT * FROM tb_member WHERE m_id=?");
	pstmt.setString(1, send_id);
	System.out.println(pstmt + "<-- pstmt");
	rs = pstmt.executeQuery();
	System.out.println(rs + "<-- rs m_list2.jsp");
	if(rs.next()){
		System.out.println("----------if 조건문 통과---------------");
		ud_id = rs.getString("m_id");
		ud_pw = rs.getString("m_pw");
		ud_level = rs.getString("m_level");
		ud_name = rs.getString("m_name");
		ud_email = rs.getString("m_email");
	}
	
}catch(SQLException ex) {
	System.out.println(ex.getMessage());
	ex.printStackTrace();
} finally {
	if (rs != null) try { rs.close(); } catch(SQLException ex) {}
	if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
	if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}

%>
<!-- 여기는 화면에 뿌려지는 표 -->
<form action="<%= request.getContextPath() %>/mupdate/m_update_pro2.jsp" method="post">
<table border="1">
<tr>
	<td>아이디</td>
	<td><input type="text" name="m_id" size="20" value = "<%=ud_id %>" readonly></td>
<tr>
<tr>
	<td>암호</td>
	<td><input type="text" name="m_pw" size="20" value = "<%=ud_pw%>"></td>
<tr>
<tr>
	<td>권한</td>
	<td><input type="text" name="m_level" size="20" value = "<%=ud_level%>"></td>
<tr>
<tr>
	<td>이름</td>
	<td><input type="text" name="m_name" size="20" value = "<%=ud_name%>"></td>
<tr>
<tr>
	<td>이메일</td>
	<td><input type="text" name="m_email" size="20" value = "<%=ud_email%>"></td>
<tr>
<tr>
	<td colspan="4"><input type="submit" value="회원수정버튼"></td>
</tr>
</table>
</form>

 

3. 수정 처리하기

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.PreparedStatement" %>
<%@ page import = "java.sql.SQLException" %>

<%
	request.setCharacterEncoding("euc-kr");
	Connection conn = null;
	PreparedStatement pstmt = null;
	
	String m_id = request.getParameter("m_id");
	String m_pw = request.getParameter("m_pw");
	String m_level = request.getParameter("m_level");
	String m_name = request.getParameter("m_name");
	String m_email = request.getParameter("m_email");
	System.out.println(m_id + "<-- m_id");
	System.out.println(m_pw + "<-- m_pw");
	System.out.println(m_level + "<-- m_level");
	System.out.println(m_name + "<-- m_name ");
	System.out.println(m_email + "<-- m_email");
	
	Class.forName("com.mysql.jdbc.Driver");
	
	String jdbcDriver = "jdbc:mysql://localhost:3306/dev35db?" +
			"useUnicode=true&characterEncoding=euckr";
	String dbUser = "dev35id";
	String dbPass = "dev35pw";
	
	conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
	System.out.println(conn + "<-- conn");
	
	pstmt = conn.prepareStatement(
			"UPDATE tb_member SET m_pw=?, m_level=?, m_name=?, m_email=? WHERE m_id = ?");
	System.out.println(pstmt + "<-- pstmt 1");
	pstmt.setString(1, m_pw);
	pstmt.setString(2, m_level);
	pstmt.setString(3, m_name);
	pstmt.setString(4, m_email);
	pstmt.setString(5, m_id);
	System.out.println(pstmt + "<-- pstmt 2");
	
%>

이렇게만 하면 화면이 빈 화면에 머물러 있다.

쿼리가 종료 된 후

response.sendRedirect(request.getContextPath() + "/mlist/m_list.jsp");

리다이렉트 명령문을 적어주면 m_list 화면으로 리다이렉트 됨

반응형

'Backend > JAVA' 카테고리의 다른 글

jdbc 로그인 처리하기  (0) 2020.03.27
jdbc - 검색하기  (0) 2020.03.27
jdbc SELECT 쿼리 실행  (0) 2020.03.20
jsp -db 연결 중 error  (0) 2020.03.19
JAVA 화면 연동 기초 이해하기  (0) 2020.03.12

댓글