DBMS : 오라클
- 이클립스와 연동도 했음
- 계정은 hr계정
DB : userj (table)
// jdbctest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.sql.* " %>
<%@ page import = "javax.sql.* " %>
<%@ page import = "javax.naming.* " %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type = "text/javascript" src="./js/script.js" charset="utf-8"></script>
</head>
<body>
<div align ="center">
<h3>이벤트 등록</h3>
<hr>
<br>
<form method="post" onsubmit = "return checkNull()" action ="jdbctest_pstmt.jsp">
등록이름 : <input type="text" id = "name" name = "name">
email주소 : <input type="text" id = "email" name = "email">
<input type="submit" value = "등록">
</form>
<br>
<hr>
</div>
# 등록 목록<br>
<%
request.setCharacterEncoding("UTF-8");
Connection conn = null;
String sql = "SELECT * FROM userj order by name desc";
try{
Context init = new InitialContext();
DataSource ds = (DataSource)init.lookup("java:comp/env/jdbc/OracleDB");
conn = ds.getConnection();
boolean f = false;
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int i = 1;
while(rs.next()){
out.println("<h3>"+(i++)+" : "+rs.getString(1)+", "+rs.getString(2)+"</h3>");
f =true;
}
// 처음에 값이 존재하지 않으면 출력
if(f==false){
out.println("<h3>값이 없음</h3>");
}
rs.close();
}catch(Exception e){
out.println("<h3>데이터 가져오기 실패 ㅋ</h3>");
e.printStackTrace();
}
%>
</body>
</html>
// jdbctest_pstmt.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.sql.* " %>
<%@ page import = "javax.sql.* " %>
<%@ page import = "javax.naming.* " %>
<%
request.setCharacterEncoding("UTF-8");
String aname = (String)request.getParameter("name");
String aemail = (String)request.getParameter("email");
Connection conn = null;
String sql = "INSERT INTO userj (name,email) VALUES (?,?)"; // ?는 나중에 채워넣겠다는 의미임.
try{
Context init = new InitialContext();
DataSource ds = (DataSource)init.lookup("java:comp/env/jdbc/OracleDB");
conn = ds.getConnection();
// 값이 있으면 속성에 맞게 값 입력
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,aname);
stmt.setString(2,aemail);
stmt.executeUpdate();
response.sendRedirect("jdbctest.jsp");
}catch(Exception e){
out.println("<h3>레코드 등록 실패</h3>");
e.printStackTrace();
}
%>
// script.js
function checkNull(){
return nullCheck();
}
// 아이디
function nullCheck(){
var name = document.getElementById("name");
var email = document.getElementById("email");
if(name.value == ""){
window.alert("이름을 입력하세요");
name.focus();
name.value = "";
return false;
}
else if(email.value == ""){
window.alert("이메일을 입력하세요");
email.focus();
email.value = "";
return false;
}
else return true;
}
반응형
'자바과정 > JSP' 카테고리의 다른 글
JSP EL 사용 예제(with 자바빈즈) (0) | 2021.03.22 |
---|---|
JSP 파일 업로드 (0) | 2021.03.12 |
JSP 회원가입 (자바빈즈 사용) (0) | 2021.03.10 |
JSP 실습 Template (0) | 2021.03.10 |
JSP 실습 application과 session (scope) (0) | 2021.03.10 |
댓글