package idc.service.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.IdcGetClientNameCode;
import utility.ReplaceHTMLChar;
import idc.model.*;

import java.sql.*;
import java.util.ArrayList;

public class IdcCSInfoCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	private int WRITING_PER_PAGE = 20;

	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		String client_code = request.getParameter("client_code");
		request.setAttribute("client_code", client_code);
		
		String service_code = request.getParameter("service_code");
		String category = request.getParameter("category");
		
		if(category==null)
			category="";
		
		String sc = request.getParameter("searchContext");
		if(sc==null)
			sc="";
		if(service_code==null)
			service_code="";
		ArrayList<IdcVPCDTO> list;
		
		int pageCnt = getPageCnt("vpcdb", category, sc);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt,curPage);
		int endPage = getEndPage(firstPage, pageCnt);

		//CSInfo리스트 받기
		list = getIdcVPCDTO(Integer.toString(curPage), service_code, category, sc);

		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("endPage", endPage);		
		request.setAttribute("vpcList", list);
				
		//kt-cs 테이블		
		ArrayList<IdcKCSDTO> kcsList;
				
		int kcs_pageCnt = getPageCnt("kcsdb", category, sc);
		int kcs_curPage = getCurPage(request.getParameter("kcs_curPage"));
		int kcs_firstPage = getFirstPage(kcs_pageCnt,kcs_curPage);
		int kcs_endPage = getEndPage(kcs_firstPage, kcs_pageCnt);
						
		kcsList = getIdcKCSDTO(Integer.toString(kcs_curPage), service_code, category, sc);
		
		request.setAttribute("kcs_pageCnt", kcs_pageCnt);
		request.setAttribute("kcs_curPage", kcs_curPage);
		request.setAttribute("kcs_firstPage", kcs_firstPage);
		request.setAttribute("kcs_endPage", kcs_endPage);
		request.setAttribute("kcsList", kcsList);
		
		
		//vpc, kcs구분	
		String csInfoFlag = request.getParameter("csInfoFlag");
		if(csInfoFlag == null) csInfoFlag = "true";
		
		request.setAttribute("csInfoFlag", csInfoFlag);
		request.setAttribute("category", category);
		request.setAttribute("searchContext", sc);
		
	}
	
	private int getCurPage(String strPage){
		int curPage=1;
		
		if(strPage !=null && strPage!="")
			curPage=Integer.parseInt(strPage);
		
		return curPage;
	}
	
	private int getPageCnt(String db, String category, String sc){
		int pageCnt=1;
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String sql="";
		
		switch(category)
		{
		case "client_name":
			sql="SELECT COUNT(*) as a FROM " + db + " as v, servicedb as s WHERE v.service_code=s.service_code AND";
			sql += " client_code = any(Select client_code from clientDB where client_name like '%"+sc+"%')";
			break;
		case "server_code":
				sql="SELECT COUNT(*) as a FROM servicedb as s, " + db + " as v WHERE s.service_code=v.service_code AND s.server_code like '%"+sc+"%' ";
			break;
		case "csip":
			if(db=="vpcdb")
				sql="SELECT COUNT(*) as a FROM " + db + " WHERE vpc_csip like '%"+sc+"%' ";
			else
				sql="SELECT COUNT(*) as a FROM " + db + " WHERE kcs_ip like '%"+sc+"%' ";
			break;
		case "service_ip":
			sql="SELECT COUNT(*) as a FROM servicedb as s, " + db + " as v WHERE s.service_code=v.service_code AND s.service_ip like '%"+sc+"%' ";
			break;
		case "cscode":
			if(db=="vpcdb")
				sql="SELECT COUNT(*) as a FROM " + db + " WHERE vpc_code like '%"+sc+"%' ";
			else
				sql="SELECT COUNT(*) as a FROM " + db + " WHERE kcs_code like '%"+sc+"%' ";
			break;
		case "connect_domain":
			if(db=="vpcdb")
				sql="SELECT COUNT(*) as a FROM " + db + " WHERE vpc_connect_domain like '%"+sc+"%' ";
			else
				sql="SELECT COUNT(*) as a FROM " + db + " WHERE kcs_connect_domain like '%"+sc+"%' ";
			break;
		default:
			sql="SELECT COUNT(*) as a FROM "+db;
		}
		try	
		{
			conn=dao.getConn();
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				int num = rs.getInt("a");				
				if(num%WRITING_PER_PAGE == 0){
					pageCnt = num/WRITING_PER_PAGE;
				}else{
					pageCnt = num/WRITING_PER_PAGE + 1;
				}
			}
			
		}catch (Exception e){
			System.out.println("IdcMemberListFormCmd - pageCnt 에서의 exception");
			e.printStackTrace();
		}finally{
			try{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();

			}catch (SQLException e){
				System.out.println("IdcMemberListFormCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	
	private int getFirstPage(int pageCnt, int curPage){
		int firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		
		int div =0;
		if(firstPage>=curPage)
			div = firstPage-curPage;
		firstPage-=div;
		
		return firstPage;
	}
	
	private int getEndPage(int firstPage, int pageCnt){
		int endPage = pageCnt;
		
		int div = pageCnt-firstPage;
		if(div>10)
			endPage-=div-10;
		return endPage;
	}	
	
	//VPC리스트 받기
	private ArrayList<IdcVPCDTO> getIdcVPCDTO(String curPage, String inservice_code, String category, String sc){
		ArrayList<IdcVPCDTO> list = new ArrayList<IdcVPCDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null, rs1 = null;
		String sql;
		
		try {
			conn = dao.getConn();
			
			sql = "SELECT count(*) AS num FROM vpcDB ";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			int numbering = 0;
			int numbering_end = WRITING_PER_PAGE * (Integer.parseInt(curPage) - 1);
			
			if(rs.next()){
				numbering = rs.getInt("num") - numbering_end;
			}
			
			switch(category)
			{
			case "client_name":
				sql="SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain, ";
				sql += " vpc_fireWall, vpc_last_date, vpc_note, t.service_code FROM vpcdb as v, servicedb as t WHERE t.service_code = v.service_code AND ";
				sql += " client_code = any(Select client_code from clientDB where client_name like '%"+sc+"%')";
				sql += " order by vpc_num desc limit ?, ?";
				break;
			case "server_code":
					sql="SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain, ";
				sql += "vpc_fireWall, vpc_last_date, vpc_note, t.service_code FROM servicedb as t, vpcdb as v WHERE t.service_code=v.service_code AND t.server_code like '%"+sc+"%' ";
				sql += " order by vpc_num desc limit ?, ?";
			//	break;
			case "mk_total":
			sql="SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain,vpc_connect_domain_dn,vpc_connect_domain_my,vpc_connect_domain_etc, ";
			sql += "vpc_fireWall, vpc_last_date, vpc_note, t.service_code FROM servicedb as t, vpcdb as v WHERE t.service_code=v.service_code ";
			sql += "AND (vpc_csip like '%"+sc+"%' or vpc_code like '%"+sc+"%' or t.service_ip like '%"+sc+"%' or  vpc_connect_domain like '%"+sc+"%' or  vpc_connect_domain_dn like '%"+sc+"%' or  vpc_connect_domain_my like '%"+sc+"%' or  vpc_connect_domain_etc like '%"+sc+"%' or client_code = any(Select client_code from clientDB where client_name like '%"+sc+"%')) ";
			sql += " order by vpc_num desc limit ?, ?";
			break;	
			case "csip":
					sql="SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain, ";
				sql += "vpc_fireWall, vpc_last_date, vpc_note, t.service_code FROM vpcdb as v, servicedb as t WHERE t.service_code=v.service_code AND vpc_csip like '%"+sc+"%' ";
				sql += " order by vpc_num desc limit ?, ?";
				break;
			case "service_ip":
				sql="SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain, ";
				sql += "vpc_fireWall, vpc_last_date, vpc_note, t.service_code FROM servicedb as t, vpcdb as v WHERE t.service_code=v.service_code AND t.service_ip like '%"+sc+"%' ";
				sql += " order by vpc_num desc limit ?, ?";
				break;
			case "cscode":
					sql="SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain, ";
				sql += "vpc_fireWall, vpc_last_date, vpc_note, t.service_code FROM vpcdb as v, servicedb as t WHERE v.service_code = t.service_code AND vpc_code like '%"+sc+"%' ";
				sql += " order by vpc_num desc limit ?, ?";
				break;
			case "connect_domain":
					sql="SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain, ";
				sql += "vpc_fireWall, vpc_last_date, vpc_note, t.service_code FROM vpcdb as v, servicedb as t WHERE v.service_code=t.service_code AND vpc_connect_domain like '%"+sc+"%' ";
				sql += " order by vpc_num desc limit ?, ?";
				break;
			default:
				sql = "SELECT vpc_num, t.service_line,t.client_code, vpc_code,vpc_csip,t.service_ip, t.server_code,vpc_connect_domain,vpc_connect_domain_dn,vpc_connect_domain_my,vpc_connect_domain_etc, ";
				sql += "vpc_fireWall, vpc_last_date, vpc_note, t.service_code ";
				sql += "from vpcDB as v, ";
				sql += "(select service_line, s.client_code,service_ip,service_code,server_code ";
				sql += "from serviceDB as s Inner join clientDB as c ";
				sql += "where s.client_code = c.client_code ";
				sql += ") as t ";
				sql += "where t.service_code = v.service_code AND t.service_code like '%"+inservice_code+"%' ";
				sql += "order by vpc_num desc limit ?, ?";
			}
			
			pstmt1 = conn.prepareStatement(sql);
			pstmt1.setInt(1, WRITING_PER_PAGE * (Integer.parseInt(curPage) -1));
			pstmt1.setInt(2, WRITING_PER_PAGE);
						
			rs1 = pstmt1.executeQuery();
			
			while(rs1.next()){
				int vpc_num = numbering--; 
				String service_line = rs1.getString("service_line");
				
				String temp ="";								
				switch (service_line)
				{
				case "normal":
					temp = "일반";
					break;
				case "office":
					temp = "자사";
					break;		
				case "event":
					temp = "이벤트";
					break;							
				case "defence":
					temp = "방어";
					break;
				case "solo":
					temp = "전용";
					break;
				case "test":
					temp = "테스트";
					break;
				case "substitution":
					temp = "대체";
					break;
				case "vpn":
					temp = "VPN";
					break;				
				}
				
				String client_name = IdcGetClientNameCode.getClientName(rs1.getString("client_code"));
				String client_code = rs1.getString("client_code");
				String vpc_code = rs1.getString("vpc_code");
				String vpc_csip = rs1.getString("vpc_csip");
				String service_ip = rs1.getString("service_ip");
				String server_code = rs1.getString("server_code");
				String vpc_connect_domain = rs1.getString("vpc_connect_domain");
				String vpc_connect_domain_dn = rs1.getString("vpc_connect_domain_dn");
				String vpc_connect_domain_my = rs1.getString("vpc_connect_domain_my");
				String vpc_connect_domain_etc = rs1.getString("vpc_connect_domain_etc");				
				String vpc_fireWall = rs1.getString("vpc_fireWall");
				Date vpc_last_date = rs1.getDate("vpc_last_date");
				String vpc_note = rs1.getString("vpc_note");
				String service_code = rs1.getString("service_code");
				
				//특수 문자 치환
				ReplaceHTMLChar rh = new ReplaceHTMLChar();
				if(vpc_connect_domain != null) vpc_connect_domain = rh.replaceHTMLChar(vpc_connect_domain);
				if(vpc_connect_domain_dn != null) vpc_connect_domain_dn = rh.replaceHTMLChar(vpc_connect_domain_dn);
				if(vpc_connect_domain_my != null) vpc_connect_domain_my = rh.replaceHTMLChar(vpc_connect_domain_my);
				if(vpc_connect_domain_etc != null) vpc_connect_domain_etc = rh.replaceHTMLChar(vpc_connect_domain_etc);
				if(vpc_fireWall != null) vpc_fireWall = rh.replaceHTMLChar(vpc_fireWall);
				if(vpc_note != null) vpc_note = rh.replaceHTMLChar(vpc_note);
				
				//줄바꿈처리
				if(vpc_connect_domain != null) vpc_connect_domain = vpc_connect_domain.replaceAll("\r\n", "<br>"); 
				if(vpc_connect_domain != null) vpc_connect_domain = vpc_connect_domain.replaceAll("\u0020","&nbsp;");
				
				if(vpc_connect_domain_dn != null) vpc_connect_domain_dn = vpc_connect_domain_dn.replaceAll("\r\n", "<br>"); 
				if(vpc_connect_domain_dn != null) vpc_connect_domain_dn = vpc_connect_domain_dn.replaceAll("\u0020","&nbsp;");
				
				if(vpc_connect_domain_my != null) vpc_connect_domain_my = vpc_connect_domain_my.replaceAll("\r\n", "<br>"); 
				if(vpc_connect_domain_my != null) vpc_connect_domain_my = vpc_connect_domain_my.replaceAll("\u0020","&nbsp;");
				
				if(vpc_connect_domain_etc != null) vpc_connect_domain_etc = vpc_connect_domain_etc.replaceAll("\r\n", "<br>"); 
				if(vpc_connect_domain_etc != null) vpc_connect_domain_etc = vpc_connect_domain_etc.replaceAll("\u0020","&nbsp;");
				
				if(vpc_fireWall != null) vpc_fireWall = vpc_fireWall.replaceAll("\r\n", "<br>");
				if(vpc_fireWall != null) vpc_fireWall = vpc_fireWall.replaceAll("\u0020","&nbsp;");
				
				if(vpc_note != null) vpc_note = vpc_note.replaceAll("\r\n", "<br>");
				if(vpc_note != null) vpc_note = vpc_note.replaceAll("\u0020","&nbsp;");								
								
				IdcVPCDTO writing = new IdcVPCDTO();
				
				writing.setVpc_num(vpc_num+"");
				writing.setService_line(service_line);
				writing.setClient_name(client_name);
				writing.setClient_code(client_code);
				writing.setVpc_code(vpc_code);
				writing.setVpc_csip(vpc_csip);
				writing.setService_ip(service_ip);
				writing.setServer_code(server_code);								
				writing.setVpc_connect_domain(vpc_connect_domain);
				writing.setVpc_connect_domain_dn(vpc_connect_domain_dn);
				writing.setVpc_connect_domain_my(vpc_connect_domain_my);
				writing.setVpc_connect_domain_etc(vpc_connect_domain_etc);
				writing.setVpc_fireWall(vpc_fireWall);
				writing.setVpc_last_date(vpc_last_date);																			
				writing.setVpc_note(vpc_note);
				writing.setService_code(service_code);
				writing.setTemp(temp);
				writing.setNum(rs1.getString("vpc_num"));
				
				list.add(writing);
			}			
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs1 != null) rs1.close();
				if(pstmt1 != null) pstmt1.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}				
		
		return list;
	}
	
	//kCS 리스트 받기
	private ArrayList<IdcKCSDTO> getIdcKCSDTO(String curPage, String service_code, String category, String sc){
		ArrayList<IdcKCSDTO> list = new ArrayList<IdcKCSDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null, rs1 = null;
		String sql;
		
		try {
			conn = dao.getConn();
			
			sql = "SELECT count(*) AS num FROM vpcDB ";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			int numbering = 0;
			int numbering_end = WRITING_PER_PAGE * (Integer.parseInt(curPage) - 1);
			
			if(rs.next()){
				numbering = rs.getInt("num") - numbering_end;
			}
			switch(category)
			{
			case "client_name":
				sql="SELECT kcs_num, t.service_line,t.client_code, kcs_code,kcs_ip,t.service_ip, t.server_code,kcs_connect_domain, ";
				sql += "kcs_use_domain, kcs_last_date, kcs_note, t.service_code, kcs_admin FROM kcsdb as k, servicedb as t WHERE t.service_code = k.service_code AND ";
				sql += " client_code = any(Select client_code from clientDB where client_name like '%"+sc+"%')";
				sql += " order by kcs_num desc limit ?, ?";
				break;
			case "server_code":
				sql="SELECT kcs_num, t.service_line,t.client_code, kcs_code,kcs_ip,t.service_ip, t.server_code,kcs_connect_domain, ";
				sql += "kcs_use_domain, kcs_last_date, kcs_note, t.service_code, kcs_admin FROM servicedb as t, kcsdb as k WHERE t.service_code=k.service_code AND t.server_code like '%"+sc+"%' ";
				sql += " order by kcs_num desc limit ?, ?";
				break;
				
			case "csip":
				sql="SELECT kcs_num, t.service_line,t.client_code, kcs_code,kcs_ip,t.service_ip, t.server_code,kcs_connect_domain, ";
				sql += "kcs_use_domain, kcs_last_date, kcs_note, t.service_code, kcs_admin FROM kcsdb as k, servicedb as t WHERE t.service_code=k.service_code AND kcs_ip like '%"+sc+"%' ";
				sql += " order by kcs_num desc limit ?, ?";
				break;
			case "service_ip":
				sql="SELECT kcs_num, t.service_line,t.client_code, kcs_code,kcs_ip,t.service_ip, t.server_code,kcs_connect_domain, ";
				sql += "kcs_use_domain, kcs_last_date, kcs_note, t.service_code, kcs_admin FROM servicedb as t, kcsdb as k WHERE t.service_code=k.service_code AND t.service_ip like '%"+sc+"%' ";
				sql += " order by kcs_num desc limit ?, ?";
				break;
			case "cscode":
				sql="SELECT kcs_num, t.service_line,t.client_code, kcs_code,kcs_ip,t.service_ip, t.server_code,kcs_connect_domain, ";
				sql += "kcs_use_domain, kcs_last_date, kcs_note, t.service_code, kcs_admin FROM kcsdb as k, servicedb as t WHERE k.service_code=t.service_code AND  kcs_code like '%"+sc+"%' ";
				sql += " order by kcs_num desc limit ?, ?";
				break;
			case "connect_domain":
				sql="SELECT kcs_num, t.service_line,t.client_code, kcs_code,kcs_ip,t.service_ip, t.server_code,kcs_connect_domain, ";
				sql += "kcs_use_domain, kcs_last_date, kcs_note, t.service_code, kcs_admin FROM kcsdb as k, servicedb as t WHERE k.service_code = t.service_code AND  kcs_connect_domain like '%"+sc+"%' ";
				sql += " order by kcs_num desc limit ?, ?";
				break;
			default:
				sql = "select kcs_num, t.service_line, kcs_admin, t.client_code, kcs_code, kcs_ip, ";
				sql += "t.service_ip, t.server_code, kcs_use_domain, kcs_connect_domain, kcs_last_date, kcs_note ";
				sql += "from kcsDB as k, ";
				sql += "(select service_line, service_admin, s.client_code, service_ip, server_code, s.service_code ";
				sql += "from serviceDB as s Inner join clientDB as c ";
				sql += "where s.client_code = c.client_code ";
				sql += ")as t ";
				sql += "where t.service_code = k.service_code AND t.service_code like '%"+service_code+"%' ";
				sql += "order by kcs_num desc limit ?, ?";
			}
			
			
			pstmt1 = conn.prepareStatement(sql);
			pstmt1.setInt(1, WRITING_PER_PAGE * (Integer.parseInt(curPage) -1));
			pstmt1.setInt(2, WRITING_PER_PAGE);
			
			rs1 = pstmt1.executeQuery();
			
			while(rs1.next()){
				int kcs_num = numbering--;
				String service_line = rs1.getString("service_line");
				String temp ="";								
				switch (service_line)
				{
				case "normal":
					temp = "일반";
					break;
				case "defence":
					temp = "방어";
					break;
				case "office":
					temp = "자사";
					break;		
				case "event":
					temp = "이벤트";
					break;								
				case "solo":
					temp = "전용";
					break;
				case "test":
					temp = "테스트";
					break;
				case "substitution":
					temp = "대체";
					break;
				case "vpn":
					temp = "VPN";
					break;				
				}
				
				String service_admin = rs1.getString("kcs_admin");
				String client_name = IdcGetClientNameCode.getClientName(rs1.getString("client_code"));
				String kcs_code = rs1.getString("kcs_code");
				String kcs_ip = rs1.getString("kcs_ip");
				String service_ip = rs1.getString("service_ip");
				String server_code = rs1.getString("server_code");
				String kcs_use_domain = rs1.getString("kcs_use_domain");
				String kcs_connect_domain = rs1.getString("kcs_connect_domain");
				Date kcs_last_date = rs1.getDate("kcs_last_date");
				String kcs_note = rs1.getString("kcs_note");
								
				//특수 문자 치환
				ReplaceHTMLChar rh = new ReplaceHTMLChar();
				
				if(kcs_use_domain !=null) kcs_use_domain = rh.replaceHTMLChar(kcs_use_domain);
				if(kcs_connect_domain != null) kcs_connect_domain = rh.replaceHTMLChar(kcs_connect_domain);
				if(kcs_note != null)kcs_note = rh.replaceHTMLChar(kcs_note);
				
				//줄바꿈처리
				if(kcs_use_domain!=null)
				{
					kcs_use_domain = kcs_use_domain.replaceAll("\r\n", "<br>");
					kcs_use_domain = kcs_use_domain.replaceAll("\u0020","&nbsp;");
				}
				if(kcs_connect_domain!=null)
				{
					kcs_connect_domain = kcs_connect_domain.replaceAll("\r\n", "<br>");
					kcs_connect_domain = kcs_connect_domain.replaceAll("\u0020","&nbsp;");
				}
				if(kcs_note!=null)				
				{
					kcs_note = kcs_note.replaceAll("\r\n", "<br>");
					kcs_note = kcs_note.replaceAll("\u0020","&nbsp;");
				}

				IdcKCSDTO writing = new IdcKCSDTO();
				writing.setNum(rs1.getString("kcs_num"));
				writing.setKcs_num(kcs_num+"");
				writing.setService_line(service_line);
				writing.setService_admin(service_admin);
				writing.setClient_name(client_name);
				writing.setKcs_code(kcs_code);
				writing.setKcs_ip(kcs_ip);
				writing.setService_ip(service_ip);
				writing.setServer_code(server_code);
				writing.setKcs_use_domain(kcs_use_domain);
				writing.setKcs_connect_domain(kcs_connect_domain);
				writing.setKcs_last_date(kcs_last_date);
				writing.setKcs_note(kcs_note);
				writing.setTemp(temp);

				list.add(writing);
			}
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs1 != null) rs1.close();
				if(pstmt1 != null) pstmt1.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}				
		
		return list;
	}

}