개발자/JSP

[JDBC] 시퀀스를 활용하여 idx값 출력 (제약조건++)

푸루닉 2022. 12. 9. 12:50

MemberDTO

package member2;

import java.sql.Date;

//	IDX			NUMBER			PRIMARY KEY,
//	USERID		VARCHAR2(100)	UNIQUE NOT NULL,
//	USERPW		VARCHAR2(255)	NOT NULL,
//	USERNAME	VARCHAR2(100)	NOT NULL,
//	JOINDATE	DATE		    DEFAULT SYSDATE,
//	GENDER		VARCHAR2(10)	CHECK(GENDER IN('남성', '여성')),
//	PROFILEIMG	VARCHAR2(255)	

public class MemberDTO {
	private int idx;
	private String userid;
	private String userpw;
	private String username;
	private Date joinDate;
	private String gender;
	private String profileImg;
	

	public int getIdx() {
		return idx;
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUserpw() {
		return userpw;
	}

	public void setUserpw(String userpw) {
		this.userpw = userpw;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Date getJoinDate() {
		return joinDate;
	}

	public void setJoinDate(Date joinDate) {
		this.joinDate = joinDate;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getProfileImg() {
		return profileImg;
	}

	public void setProfileImg(String profileImg) {
		this.profileImg = profileImg;
	}
	
	

}

MemberDAO

package member2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MemberDAO {
	
	private final String url = "jdbc:oracle:thin:@192.168.1.100:1521:xe";
	private final String user = "c##itbank";
	private final String password = "it";
	

	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	public MemberDAO() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(url, user, password);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public int insert(MemberDTO dto) {
		int row = 0;
		String sql = "insert into member2 (userid, userpw, username, gender) values (?, ?, ?, ?)";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, dto.getUserid());
			pstmt.setString(2, dto.getUserpw());
			pstmt.setString(3, dto.getUsername());
			pstmt.setString(4, dto.getGender());
			row = pstmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch(Exception e) {}
		}
		return row;
	}

}

삽입

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>day12</title>
</head>
<body>

<h1>member2 추가하기</h1>
<hr>

<form method="POST" action="ex01-action.jsp">
	<p><input type="text" name="userid" placeholder="ID"></p>
	<p><input type="password" name="userpw" placeholder="Password"></p>
	<p><input type="text" name="username" placeholder="Name"></p>
	<p>
		<label><input type="radio" name="gender" value="남성">남성</label>
		<label><input type="radio" name="gender" value="여성">여성</label>
	</p>
	<p><input type="submit"></p>
</form>



</body>
</html>

삽입 액션

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ex01-action.jsp</title>
</head>
<body>
<%	request.setCharacterEncoding("UTF-8"); %>	

<jsp:useBean id="dao" class="member2.MemberDAO" />

<jsp:useBean id="user" class="member2.MemberDTO" />
<jsp:setProperty property="*" name="user"/>

<c:set var="row" value="${dao.insert(user) }" />

<h3>${row != 0 ? '성공' : '실패' }</h3>

</body>
</html>

 

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

[JDBC] SingleTon 패턴  (0) 2022.12.12
[JDBC] XML  (0) 2022.12.12
[SQL] DB Table 제약조건  (0) 2022.12.09
[JDBC] 자바와 sql 연결 후 출력하는 방법  (0) 2022.12.06
[JSP] 세션(session)  (0) 2022.12.05