티스토리 뷰
# db연동
1. jdbc드라이버를 아래경로에 추가하기
플젝명/WEB-INF/lib/ jdbc드라이버.jar
2. server.xml에 Resource태그 추가하기
server.xml
==========
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxIdle="10" maxTotal="20" maxWaitMillis="-1" name="jdbc/myoracle" password="tiger" type="javax.sql.DataSource" url="jdbc:oracle:thin:@localhost:1521:orcl" username="scott"/>
3. context.xml에 ResourceLink태그 추가하기
context.xml
===========
<ResourceLink global="jdbc/myoracle" name="jdbc/myoracle" type="javax.sql.DataSource"/>
4. Conenection객체 얻기
ConnUtil.java
=============
package kr.co.seoulit.common.util;
import java.sql.SQLException;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnUtil{
public static Connection getConnection() throws SQLException, NamingException, ClassNotFoundException{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
return conn;
}
}
5. jsp페이지작성
<%
Connection conn=null;
PreparedStatement allStmt=null;
ResultSet rs=null;
try{
conn=ConnUtil.getConnection();
String all="select * from member";
allStmt=conn.prepareStatement(all);
rs=allStmt.executeQuery();
while(rs.next()){
String id=rs.getString("id");
out.println(id+"<br/>");
}
}finally{
if(rs!=null){ rs.close();rs=null; }
if(allStmt!=null){allStmt.close();allStmt=null; }
if(conn!=null){ conn.close();conn=null; }
}
%>
# db연동과 관련된 예제
[ex] 아래와 같이 요청했을때 emp테이블의 ename칼럼의 값이
출력될 수 있도록 소스코드를 작성하시오.
http://localhost:8282/p1/a.jsp?empno=7369
SMITH
[an]
a.jsp
=====
<%
String empno=request.getParameter("empno");
Connection conn=null;
PreparedStatement allStmt=null;
ResultSet rs=null;
try{
conn=ConnUtil.getConnection();
String all="select ename from emp where empno=?";
allStmt=conn.prepareStatement(all);
allStmt.setString(1,empno);
rs=allStmt.executeQuery();
if(rs.next()){
out.println(rs.getString("ename"));
}
}finally{
if(rs!=null){ rs.close();rs=null; }
if(allStmt!=null){allStmt.close();allStmt=null; }
if(conn!=null){ conn.close();conn=null; }
}
%>
[ex] 아래와 같이 요청했을때 member테이블의 id칼럼의 값이
모두 출력될 수 있도록 소스코드를 작성하시오.
http://localhost:8282/p1/a.jsp
id1
id2
id3
id4
id5
[an]
a.jsp
=====
<%
Connection conn=null;
PreparedStatement allStmt=null;
ResultSet rs=null;
try{
conn=ConnUtil.getConnection();
String all="select id from member";
allStmt=conn.prepareStatement(all);
rs=allStmt.executeQuery();
while(rs.next()){
out.println(rs.getString("id")+"<br/>");
}
}finally{
if(rs!=null){ rs.close();rs=null; }
if(allStmt!=null){allStmt.close();allStmt=null; }
if(conn!=null){ conn.close();conn=null; }
}
%>
[ex] emp테이블의 empno를 텍스트박스에 입력한뒤
"직원검색" 버튼누르면 "url"이 아래와 같이 변경되게 하려면?
http://localhost:8282/p1/b.jsp?empno=7369
<h1>직원의no를 입력하세요</h1>
<input type="text"><br/>
<input type="submit" value="직원검색">
[an]
<form action="b.jsp">
<h1>직원의no를 입력하세요</h1>
<input type="text" name="empno"><br/>
<input type="submit" value="직원검색">
</form>
[ex] 아래와 같이 요청했을때 member테이블의 id칼럼의 값이
모두 출력될 수 있도록 소스코드를 작성하시오.
http://localhost:8282/p1/a.jsp
단, 응답된 소스코드가 아래와 같아야한다.
id1,id2,id3은 member테이블의 id칼럼의 값이다.
<input type="radio" name="id" value="id1">id1</a><br/>
<input type="radio" name="id" value="id2">id2</a><br/>
<input type="radio" name="id" value="id3">id3</a><br/>
[an]
<%
Connection conn=null;
PreparedStatement allStmt=null;
ResultSet rs=null;
try{
conn=ConnUtil.getConnection();
String all="select id from member";
allStmt=conn.prepareStatement(all);
rs=allStmt.executeQuery();
while(rs.next()){
String id=rs.getString("id");
out.println("<input type=radio name=id value="+id+">");
out.println(id);
out.println("<br/>");
}
}finally{
if(rs!=null){ rs.close();rs=null; }
if(allStmt!=null){allStmt.close();allStmt=null; }
if(conn!=null){ conn.close();conn=null; }
}
%>
[ex] 아래와 같이 요청했을때 member테이블의 id칼럼의 값이
모두 출력될 수 있도록 소스코드를 작성하시오.
http://localhost:8282/p1/a.jsp
단, 응답된 소스코드가 아래와 같아야한다.
id1,id2,id3은 member테이블의 id칼럼의 값이다.
<a href="b.jsp?id=id1">id1</a><br/>
<a href="b.jsp?id=id2">id2</a><br/>
<a href="b.jsp?id=id3">id3</a><br/>
[an]
<%
Connection conn=null;
PreparedStatement allStmt=null;
ResultSet rs=null;
try{
conn=ConnUtil.getConnection();
String all="select id from member";
allStmt=conn.prepareStatement(all);
rs=allStmt.executeQuery();
while(rs.next()){
String id=rs.getString("id");
out.println("<a href=b.jsp?id="+id+">");
out.println(id);
out.println("<a/><br/>");
}
}finally{
if(rs!=null){ rs.close();rs=null; }
if(allStmt!=null){allStmt.close();allStmt=null; }
if(conn!=null){ conn.close();conn=null; }
}
%>
[ex] 텍스트박스에 각각id와 pw를 입력한뒤 "로그인"버튼을 누르면
아래와 같은 결과가 출력될 수 있도록 "b.jsp"를 완성하시오.
존재하지 않는 ID입니다.
비번이 잘못되었습니다.
로그인성공했습니다.
a.jsp
=====
<form action="b.jsp">
<input type="text" name="id"><br/>
<input type="text" name="pw"><br/>
<input type="submit" value="로그인">
</form>
[an]
b.jsp
=====
<%@page import="kr.co.seoulit.common.util.ConnUtil"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id=request.getParameter("id");
String pw=request.getParameter("pw");
Connection conn=null;
PreparedStatement allStmt=null;
ResultSet rs=null;
try{
conn=ConnUtil.getConnection();
String all="select * from member where id=?";
allStmt=conn.prepareStatement(all);
allStmt.setString(1,id);
rs=allStmt.executeQuery();
if(rs.next()){
String dbpw=rs.getString("pw");
if(pw.equals(dbpw)){
out.println("로그인성공했습니다.");
}else{
out.println("비밀번호일치하지않습니다.");
}
}else{
out.println("존재하지 않는 ID입니다.");
}
}finally{
if(rs!=null){ rs.close();rs=null; }
if(allStmt!=null){allStmt.close();allStmt=null; }
if(conn!=null){ conn.close();conn=null; }
}
%>
</body>
</html>
[ex] 아래와 같이 요청했을때 id파라메터에 해당하는
비번,주소,전번을 텍스트박스에 출력되게 하시오.
http://localhost:8282/p1/a.jsp?id=aa
<h1>회원정보수정폼</h1>
ID : <input type="text" disabled><br/>
PW : <input type="text"><br/>
ADDR : <input type="text"><br/>
TEL : <input type="text"><br/>
[an]
<%
String str=request.getParameter("id");
String id="",pw="",addr="",tel="";
Connection conn=null;
PreparedStatement allStmt=null;
ResultSet rs=null;
try{
conn=ConnUtil.getConnection();
String all="select * from member where id=?";
allStmt=conn.prepareStatement(all);
allStmt.setString(1,str);
rs=allStmt.executeQuery();
if(rs.next()){
id=rs.getString("id");
pw=rs.getString("pw");
addr=rs.getString("addr");
tel=rs.getString("tel");
}
}finally{
if(rs!=null){ rs.close();rs=null; }
if(allStmt!=null){allStmt.close();allStmt=null; }
if(conn!=null){ conn.close();conn=null; }
}
%>
<h1>회원정보수정폼</h1>
ID : <input type="text" disabled value="<%=id%>"><br/>
PW : <input type="text" value="<%=pw%>"><br/>
ADDR : <input type="text" value="<%=addr%>"><br/>
TEL : <input type="text" value="<%=tel%>"><br/>
'VIEW > JSP&SERVLET' 카테고리의 다른 글
[JSP] jquery (0) | 2017.12.01 |
---|---|
[JSP] application,session,화면이동,영역정리 (0) | 2017.12.01 |
[JSP] request객체 (0) | 2017.12.01 |
[JSP]톰캣 구조,_jspService메서드,jsp태그,jsp 내장객체 (0) | 2017.12.01 |
[JSP] JSP & servlet 이란? (0) | 2017.12.01 |