JSP DAO分離JOIN CODE
6682 ワード
VOを使用する前にDAOを接続します.jspにはコードが全部入っています.
DAO分離とVOを使用する前に、1ページにコードを書きすぎます.
join.jsp
メンテナンスとリサイクルのために、コードを別々に管理します.
jsp、DAO、VOを使用してページング
join.jsp
DAO分離とVOを使用する前に、1ページにコードを書きすぎます.
join.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="kr.co.koo.jdbc.user.model.UserDAO"%>
<%@ page import="java.sql.*" %>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String phone1 = request.getParameter("phone1");
String phone2 = request.getParameter("phone2");
String phone3 = request.getParameter("phone3");
String gender = request.getParameter("gender");
//2. JDBC로직 작성.
String url = "jdbc:mysql://localhost:3306/jsp_practice";
String uid = "jsp";
String upw = "jsp";
Connection conn = null;
//Statement stmt = null;
PreparedStatement pstmt = null;
String sql = "INSERT INTO users VALUES ('"
+ name + "', '" + id + "', '" + pw
+ "', '" + phone1 + "', '" + phone2
+ "', '"+ phone3 + "', '" + gender + "')";
String sql = "INSERT INTO users VALUES (?,?,?,?,?,?,?)";
System.out.println(sql);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, uid, upw);
//stmt = conn.createStatement();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, id);
pstmt.setString(3, pw);
pstmt.setString(4, phone1);
pstmt.setString(5, phone2);
pstmt.setString(6, phone3);
pstmt.setString(7, gender);
//int rn = stmt.executeUpdate(sql);
int rn = pstmt.executeUpdate();
if(rn == 1) {
session.setAttribute("user_name", name);
response.sendRedirect("join_success.jsp");
} else {
response.sendRedirect("join_form.jsp");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
conn.close(); pstmt.close();//stmt.close();
} catch(Exception e) {
e.printStackTrace();
}
}
%>
-コードのメンテナンスと回収が難しく、UPが不便です。
メンテナンスとリサイクルのために、コードを別々に管理します.
jsp、DAO、VOを使用してページング
join.jsp
<jsp:useBean id="users" class="kr.co.koo.jdbc.user.model.UserVO" />
<jsp:setProperty name = "users" property = "*"/>
<%
UserDAO dao = UserDAO.getInstance();
int rn = dao.join(users);
if(rn == 1){
session.setAttribute("user_name",users.getName());
response.sendRedirect("joim_success.jsp");
} else {
response.sendRedirect("join_form.jsp");
}
%>
vo.javapackage kr.co.koo.jdbc.user.model;
public class UserVO {
private String name;
private String id;
private String pw;
private String phone1;
private String phone2;
private String phone3;
private String gender;
public UserVO() {}
public UserVO(String name, String id, String pw, String phone1, String phone2, String phone3, String gender) {
super();
this.name = name;
this.id = id;
this.pw = pw;
this.phone1 = phone1;
this.phone2 = phone2;
this.phone3 = phone3;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getPhone1() {
return phone1;
}
public void setPhone1(String phone1) {
this.phone1 = phone1;
}
public String getPhone2() {
return phone2;
}
public void setPhone2(String phone2) {
this.phone2 = phone2;
}
public String getPhone3() {
return phone3;
}
public void setPhone3(String phone3) {
this.phone3 = phone3;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
dao.javapackage kr.co.koo.jdbc.user.model;
import java.sql.*;
import java.util.*;
public class UserDAO {
private String url = "jdbc:mysql://localhost:3306/";
private String uid = "";
private String upw = "";
private UserDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(Exception e) {
e.printStackTrace();
}
}
private static UserDAO dao = new UserDAO();
public static UserDAO getInstance() {
if(dao != null) {
dao = new UserDAO();
}
return dao;
}
public int join(UserVO users) {
String sql = "INSERT INTO users VALUES (?,?,?,?,?,?,?)";
Connection conn = null;
PreparedStatement pstmt = null;
int rn = 0;
try {
conn = DriverManager.getConnection(url, uid, upw);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, users.getName());
pstmt.setString(2, users.getId());
pstmt.setString(3, users.getPw());
pstmt.setString(4, users.getPhone1());
pstmt.setString(5, users.getPhone2());
pstmt.setString(6, users.getPhone3());
pstmt.setString(7, users.getGender());
rn = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close(); pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return rn;
}
Reference
この問題について(JSP DAO分離JOIN CODE), 我々は、より多くの情報をここで見つけました https://velog.io/@jominjun94/JSP-DAO분리-JOIN-CODEテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol