ConnectionPool로 연결하고 사용하기
package board.model;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import common.base.DAOBase;
import jdbc.util.DBUtil;
public class BoardDAO extends DAOBase{
private DataSource ds;
public BoardDAO() {
//server.xml에 등록외어 있는 커넥션 풀을 룩업하자
try {
Context ctx = new InitialContext();
//1.WAS서버를 찾자 (톰캣)
Context ectx =(Context)ctx.lookup("java:comp/env");
//톰캣찾는 프로토콜
//2.우리가 등록한 DataSource를 찾자. - resource name으로 룩업한다. => JNDI (Java Naming Directory Interface)
ds=(DataSource)ectx.lookup("jdbc/myshop");
System.out.println("DataSource Lookup Seccess!!");
} catch (NamingException e) {
System.out.println("데이터 소스 룩업 실패");
e.printStackTrace();
}
}
public void deleteBoard(String idx) throws SQLException{
try {
//con=DBUtil.getCon();
con=ds.getConnection();
String sql="delete from board where idx=?";
ps=con.prepareStatement(sql);
ps.setString(1, idx);
int n = ps.executeUpdate();
}finally {
close();
}
}
}
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
Ajax
책검색
GET방식 한글처리
encodeURIComponent(val) 한글 처리
var bookInfoByPub=function(val){
//alert(val);
//자바스크립트에서 URI부분의 한글 인코드 처리해주는 함수 => encodeURIComponent()함수 사용
$.ajax({
type:'GET',
url:'bookTitle.jsp?publish='+encodeURIComponent(val),
dataType:'html',
cache:false,
success:function(res){
alert(res);
},error:function(err){
alert('error: '+err.status);
}
})
}
bookTitle.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,ajax.book.*"%>
<jsp:useBean id="bDao" class="ajax.book.BookDAO" scope="session"/>
<%
String publish = request.getParameter("publish");
List<BookDTO> arr= bDao.getTitleList(publish);
%>
<select id="selTitle" name="selTitle" onchange="">
<option value="">::도서목록::</option>
<%
for(BookDTO b:arr){
%>
<option value="<%=b.getTitle()%>"><%=b.getTitle()%></option>
<%
}
%>
</select>
자동완성
onkeyup
<input type="text" name="books" id="books" onkeyup="autoComp(this.value)" class="form-control" >
ajax
var autoComp = function(val){
//console.log("val="+val);
$.ajax({
type:'POST',
url:'autoComplete.jsp',
data:'findKeyword='+val,//요청 파라미터
dataType:'html',
success:function(res){
$('#lst2').html(res);
$('#lst1').show(100);
$('#lst2').show(100);
if(!$('#books').val()){
$('#lst1').hide(100);
$('#lst2').hide(100);
}
},error:function(err){
alert("error: "+err);
}
})
}
var setting=function(val){
$("#books").val(val);
$('#lst1').hide(100);
$('#lst2').hide(100);
}
autoComplete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,ajax.book.*"%>
<jsp:useBean id="bDao" class="ajax.book.BookDAO" scope="session"/>
<%
//검색어 받기
String keyword=request.getParameter("findKeyword");
if(keyword!=null){
keyword=keyword.trim();
}
List<BookDTO> arr= bDao.getFindBook(keyword);
if(arr!=null){
for(BookDTO b:arr){
%>
<li><a href="#books" onclick="setting('<%=b.getTitle()%>')"><%=b.getTitle()%></a></li>
<%
}
}
%>