<script type="text/javascript">
$(function() {
$("#hp2").on('keyup',function(){
if(this.value.length==4){
$("#hp3").focus();
}
})
})
</script>
if(list.size()==0){
%>
<tr>
<td colspan="7" align="center">
<h3>데이터가 없습니다</h3>
</td>
</tr>
<%}
<script>
$(".name").each(function(){
$(this).mouseover(function(){
$(this).css({
"cursor":"pointer",
"text-decoration":"underline",
"text-decoration-color":"cadetblue",
"color":"cadetblue"
})
})
$(this).mouseout(function(){
$(this).css({
"cursor":"pointer",
"text-decoration":"none",
"color":"black"
})
})
})
<tr>
<th>혈액형</th>
<td>
<select name="blood">
<option value="A형"
<%=dto.getBlood().equals("A형")?"selected":""%>>A형</option>
<option value="AB형"
<%=dto.getBlood().equals("AB형")?"selected":""%>>AB형</option>
<option value="B형"
<%=dto.getBlood().equals("B형")?"selected":""%>>B형</option>
<option value="O형"
<%=dto.getBlood().equals("O형")?"selected":""%>>O형</option>
</select>
</td>
</tr>
split으로 쪼갤 경우 그 값은 배열을 반환하기에 배열에 담아줘야한다
String arr[]=dto.getHp().split("-");
<tr>
<th>전화번호</th>
<td>
<select name="hp1">
<option value="010"
<%=arr[0].equals("010")?"selected":""%>>010</option>
<option value="02"
<%=arr[0].equals("02")?"selected":""%>>02</option>
<option value="031"
<%=arr[0].equals("031")?"selected":""%>>031</option>
<option value="011"
<%=arr[0].equals("011")?"selected":""%>>011</option>
<option value="016"
<%=arr[0].equals("016")?"selected":""%>>016</option>
</select>
<b>-</b>
<input type="text" name="hp2" size="4" maxlength="4"
required="required" style="width: 100px;" placeholder="가운데 자리" id="hp2"
value="<%=arr[1]%>">
<b>-</b>
<input type="text" name="hp3" size="4" maxlength="4"
required="required" style="width: 100px;" placeholder="마지막 자리" id="hp3"
value="<%=arr[2]%>">
</td>
</tr>
create table intro(intro_num number(5) primary key,
intro_name varchar2(20),
intro_blood varchar2(20),
intro_hp varchar2(20),
intro_city varchar2(50),
gaipday date);
select * from intro;
package oracle.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnect {
static final String ORACLE_URL="jdbc:oracle:thin:@localhost:1521:XE";
//driver
String driver="oracle.jdbc.driver.OracleDriver";
public DBConnect() {
try {
Class.forName(driver);
System.out.println("오라클 드라이버 성공!!!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("오라클 드라이버 실패!!!");
}
}
//Connection
public Connection getConnection()
{
Connection conn=null;
try {
conn=DriverManager.getConnection(ORACLE_URL, "tjdgus", "1234");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("오라클 연결 실패: url,계정,비밀번호 확인 요함! "+e.getMessage());
}
return conn;
}
//close메서드..총 4개
//완성구문 2개
public void dbClose(ResultSet rs,Statement stmt,Connection conn)
{
try {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void dbClose(Statement stmt,Connection conn)
{
try {
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//미완구문 2개 //preparedstatement 나중에 정의해줌
public void dbClose(ResultSet rs,PreparedStatement pstmt,Connection conn)
{
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void dbClose(PreparedStatement pstmt,Connection conn)
{
try {
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package model.intro;
import java.sql.Timestamp;
public class IntroDto {
private String num;
private String name;
private String blood;
private String hp;
private String city;
private Timestamp gaipday;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBlood() {
return blood;
}
public void setBlood(String blood) {
this.blood = blood;
}
public String getHp() {
return hp;
}
public void setHp(String hp) {
this.hp = hp;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Timestamp getGaipday() {
return gaipday;
}
public void setGaipday(Timestamp gaipday) {
this.gaipday = gaipday;
}
}
package model.intro;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import oracle.db.DBConnect;
public class IntroDao {
DBConnect db=new DBConnect();
public void insertIntro(IntroDto dto)
{
Connection conn=db.getConnection();
PreparedStatement pstmt=null;
String sql="insert into intro values(seq_1.nextval,?,?,?,?,sysdate)";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, dto.getName());
pstmt.setString(2, dto.getBlood());
pstmt.setString(3, dto.getHp());
pstmt.setString(4, dto.getCity());
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.dbClose(pstmt, conn);
}
}
public Vector<IntroDto> getAllIntro()
{
Vector<IntroDto> list=new Vector<IntroDto>();
Connection conn=db.getConnection();
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select * from intro order by intro_num";
try {
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
IntroDto dto=new IntroDto();
dto.setNum(rs.getString("intro_num"));
dto.setName(rs.getString("intro_name"));
dto.setBlood(rs.getString("intro_blood"));
dto.setHp(rs.getString("intro_hp"));
dto.setCity(rs.getString("intro_city"));
dto.setGaipday(rs.getTimestamp("gaipday"));
list.add(dto);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.dbClose(rs, pstmt, conn);
}
return list;
}
public void deleteIntro(String num)
{
Connection conn=db.getConnection();
PreparedStatement pstmt=null;
String sql="delete from intro where intro_num=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, num);
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.dbClose(pstmt, conn);
}
}
public IntroDto getData(String num)
{
IntroDto dto=new IntroDto();
Connection conn=db.getConnection();
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select * from intro where intro_num=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, num);
rs=pstmt.executeQuery();
if(rs.next())
{
dto.setNum(rs.getString("intro_num"));
dto.setName(rs.getString("intro_name"));
dto.setBlood(rs.getString("intro_blood"));
dto.setHp(rs.getString("intro_hp"));
dto.setCity(rs.getString("intro_city"));
dto.setGaipday(rs.getTimestamp("gaipday"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
db.dbClose(rs, pstmt, conn);
}
return dto;
}
public void updateIntro(IntroDto dto)
{
Connection conn=db.getConnection();
PreparedStatement pstmt=null;
String sql="update intro set intro_name=?,intro_blood=?,intro_hp=?,intro_city=? where intro_num=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, dto.getName());
pstmt.setString(2, dto.getBlood());
pstmt.setString(3, dto.getHp());
pstmt.setString(4, dto.getCity());
pstmt.setString(5, dto.getNum());
pstmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
db.dbClose(pstmt, conn);
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
$(function() {
$("#hp2").on('keyup',function(){
if(this.value.length==4){
$("#hp3").focus();
}
})
})
</script>
</head>
<body>
<form action="insertAction.jsp" method="post">
<table class="table table-bordered" style="width: 500px;">
<b>개인정보 입력창</b>
<tr>
<th>이름</th>
<td>
<input type="text" name="name" class="form-control"
required="required" style="width: 150px;" placeholder="이름">
</td>
</tr>
<tr>
<th>혈액형</th>
<td>
<select name="blood">
<option value="A형">A형</option>
<option value="AB형">AB형</option>
<option value="B형">B형</option>
<option value="O형">O형</option>
</select>
</td>
</tr>
<tr>
<th>전화번호</th>
<td>
<select name="hp1">
<option value="010">010</option>
<option value="02">02</option>
<option value="031">031</option>
<option value="011">011</option>
<option value="016">016</option>
</select>
<b>-</b>
<input type="tel" name="hp2" size="4" maxlength="4"
required="required" style="width: 100px;" placeholder="가운데 자리" id="hp2">
<b>-</b>
<input type="tel" name="hp3" size="4" maxlength="4"
required="required" style="width: 100px;" placeholder="마지막 자리" id="hp3">
</td>
</tr>
<tr>
<th>도시</th>
<td>
<input type="radio" name="city" value="서울">서울
<input type="radio" name="city" value="부산">부산
<input type="radio" name="city" value="경기">경기
<input type="radio" name="city" value="인천">인천
<input type="radio" name="city" value="제주">제주
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="submit" class="btn btn-info">전송</button>
<button type="button" class="btn btn-success"
onclick="location.href='list.jsp'">목록</button>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="model.intro.IntroDto"%>
<%@page import="java.util.Vector"%>
<%@page import="model.intro.IntroDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
</head>
<body>
<%
IntroDao dao=new IntroDao();
Vector<IntroDto> list=dao.getAllIntro();
%>
<button type="button" class="btn btn-info"
onclick="location.href='insertForm.jsp'">데이터 추가</button>
<br><br>
<table class="table table-bordered" style="width: 900px;">
<tr align="center" valign="middle" class="table-info">
<th width="60">번호</th>
<th width="80">이름</th>
<th width="80">혈액형</th>
<th width="150">전화번호</th>
<th width="80">도시</th>
<th width="250">가입날짜</th>
<th width="250">편집</th>
</tr>
<%
if(list.size()==0){
%>
<tr>
<td colspan="7" align="center">
<h3>데이터가 없습니다</h3>
</td>
</tr>
<%}
else
{
for(int i=0;i<list.size();i++)
{
IntroDto dto=list.get(i);
%>
<tr>
<td><%=i+1 %></td>
<td class="name" onclick="location.href='detail.jsp?num=<%=dto.getNum()%>'"><%=dto.getName() %></td>
<td><%=dto.getBlood() %></td>
<td><%=dto.getHp() %></td>
<td><%=dto.getCity() %></td>
<td><%=dto.getGaipday() %></td>
<td>
<button type="button" class="btn btn-success btn-sm"
onclick="location.href='updateForm.jsp?num=<%=dto.getNum()%>'">수정</button>
<button type="button" class="btn btn-danger btn-sm"
onclick="confirm('삭제하겠습니까?')?location.href='delete.jsp?num=<%=dto.getNum()%>'
:alert('취소하셨습니다')">삭제</button>
</td>
</tr>
<%}
}
%>
</table>
<script>
$(".name").each(function(){
$(this).mouseover(function(){
$(this).css({
"cursor":"pointer",
"text-decoration":"underline",
"text-decoration-color":"cadetblue",
"color":"cadetblue"
})
})
$(this).mouseout(function(){
$(this).css({
"cursor":"pointer",
"text-decoration":"none",
"color":"black"
})
})
})
</script>
</body>
</html>
<%@page import="model.intro.IntroDao"%>
<%@page import="model.intro.IntroDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String blood=request.getParameter("blood");
String hp=request.getParameter("hp1")+"-"+request.getParameter("hp2")+"-"+request.getParameter("hp3");
String city=request.getParameter("city");
IntroDto dto=new IntroDto();
IntroDao dao=new IntroDao();
dto.setName(name);
dto.setBlood(blood);
dto.setHp(hp);
dto.setCity(city);
dao.insertIntro(dto);
response.sendRedirect("list.jsp");
%>
</body>
</html>
<%@page import="model.intro.IntroDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
</head>
<body>
<%
String num=request.getParameter("num");
IntroDao dao=new IntroDao();
dao.deleteIntro(num);
response.sendRedirect("list.jsp");
%>
</body>
</html>
<%@page import="model.intro.IntroDto"%>
<%@page import="model.intro.IntroDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
$(function() {
$("#hp2").on('keyup',function(){
if(this.value.length==4){
$("#hp3").focus();
}
})
})
</script>
<%
String num=request.getParameter("num");
IntroDao dao=new IntroDao();
IntroDto dto=dao.getData(num);
String arr[]=dto.getHp().split("-");
%>
</head>
<body>
<form action="updateAction.jsp" method="post">
<input type="hidden" name="num" value="<%=num %>">
<table class="table table-bordered" style="width: 500px;">
<b>개인정보 입력창</b>
<tr>
<th>이름</th>
<td>
<input type="text" name="name" class="form-control"
required="required" style="width: 150px;" placeholder="이름"
value="<%=dto.getName()%>">
</td>
</tr>
<tr>
<th>혈액형</th>
<td>
<select name="blood">
<option value="A형"
<%=dto.getBlood().equals("A형")?"selected":""%>>A형</option>
<option value="AB형"
<%=dto.getBlood().equals("AB형")?"selected":""%>>AB형</option>
<option value="B형"
<%=dto.getBlood().equals("B형")?"selected":""%>>B형</option>
<option value="O형"
<%=dto.getBlood().equals("O형")?"selected":""%>>O형</option>
</select>
</td>
</tr>
<tr>
<th>전화번호</th>
<td>
<select name="hp1">
<option value="010"
<%=arr[0].equals("010")?"selected":""%>>010</option>
<option value="02"
<%=arr[0].equals("02")?"selected":""%>>02</option>
<option value="031"
<%=arr[0].equals("031")?"selected":""%>>031</option>
<option value="011"
<%=arr[0].equals("011")?"selected":""%>>011</option>
<option value="016"
<%=arr[0].equals("016")?"selected":""%>>016</option>
</select>
<b>-</b>
<input type="text" name="hp2" size="4" maxlength="4"
required="required" style="width: 100px;" placeholder="가운데 자리" id="hp2"
value="<%=arr[1]%>">
<b>-</b>
<input type="text" name="hp3" size="4" maxlength="4"
required="required" style="width: 100px;" placeholder="마지막 자리" id="hp3"
value="<%=arr[2]%>">
</td>
</tr>
<tr>
<th>도시</th>
<td>
<input type="radio" name="city" value="서울"
<%=dto.getCity().equals("서울")?"checked":""%>>서울
<input type="radio" name="city" value="부산"
<%=dto.getCity().equals("부산")?"checked":""%>>부산
<input type="radio" name="city" value="경기"
<%=dto.getCity().equals("경기")?"checked":""%>>경기
<input type="radio" name="city" value="인천"
<%=dto.getCity().equals("인천")?"checked":""%>>인천
<input type="radio" name="city" value="제주"
<%=dto.getCity().equals("제주")?"checked":""%>>제주
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="submit" class="btn btn-info"
onclick="location.href='list.jsp'">수정</button>
<button type="button" class="btn btn-success"
onclick="location.href='list.jsp'">목록</button>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="model.intro.IntroDao"%>
<%@page import="model.intro.IntroDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String num=request.getParameter("num");
String name=request.getParameter("name");
String blood=request.getParameter("blood");
String hp=request.getParameter("hp1")+"-"+request.getParameter("hp2")+"-"+request.getParameter("hp3");
String city=request.getParameter("city");
IntroDto dto=new IntroDto();
IntroDao dao=new IntroDao();
dto.setNum(num);
dto.setName(name);
dto.setBlood(blood);
dto.setHp(hp);
dto.setCity(city);
dao.updateIntro(dto);
response.sendRedirect("list.jsp");
%>
</body>
</html>
<%@page import="model.intro.IntroDto"%>
<%@page import="model.intro.IntroDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cute+Font&family=Diphylleia&family=Dokdo&family=Nanum+Brush+Script&family=Nanum+Gothic+Coding&family=Noto+Sans+KR&display=swap" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String num=request.getParameter("num");
IntroDao dao=new IntroDao();
IntroDto dto=dao.getData(num);
%>
<table class="table table-bordered" style="width: 700px;">
<b>상세 정보</b>
<tr align="center" valign="middle">
<th>이름</th>
<td><%=dto.getName() %></td>
</tr>
<tr align="center" valign="middle">
<th>지역</th>
<td><%=dto.getCity() %></td>
</tr>
<tr align="center" valign="middle">
<th>전화번호</th>
<td><%=dto.getHp() %></td>
</tr>
<tr align="center" valign="middle">
<th>혈액형</th>
<td><%=dto.getBlood() %></td>
</tr>
<tr align="center" valign="middle">
<th>가입일</th>
<td><%=dto.getGaipday() %></td>
</tr>
<tr>
<td colspan="2" align="center">
<button class="btn btn-success" style="width: 150px;"
onclick="location.href='list.jsp'">목록</button>
</td>
</tr>
</table>
</body>
</html>