JqGrid를 이용해서 회원정보를 CRUD를 했다

날짜컬럼을 update하면 sqlException오류가 발생했다 

날짜컬럼에 YYYY-mm-dd로 입력하면 db까지 같은 형식으로 저장이 되는데

client side에서 보면 mm/dd/YYYY로 변환이 된다

날짜 형식이 달라서 update가 되지 않는 것이다

 

formatter속성으로 데이터를 조작해도 날짜 형식이 변하지 않았다

구글링을 했더니 server side에서 데이터를 조작하던지 custom formatter로 설정하라는 의견이 많았다

date custom formatter를 사용했지만 변수가 많았고 어려웠다

그래서 server side에서 데이터를 변환해보았다

 

crud가 잘된다

2019/07/18 - [웹/JAVA] - Try Catch문으로 오류잡기

 

Try Catch문으로 오류잡기

포워딩(전송)을 하게되면 오류가 발생할 수 있는데 이 오류를 두 가지로 나눌 수 있다. 컴파일오류와 실행오류이다. 컴파일오류는 오타를 쳤을때고 Java에서 알려준다. 실행오류는 예를 들어 무한 for문이다. 내가..

golddigger.tistory.com

 

 

 

실무에서는 syso 사용을 안한다. syso을 개발서버나 다른서버에 올리면 안된다. 굉장히 속도가 느리기 때문이다. 

그래서 개인 개발용으로만 사용하는 것이 좋다


Logger를 써야한다. 얘는 한줄만 있어야 한다. 
1. log.info( ); 
2. log.debug( );  
시간과 무엇으로 그리고 어디에서 찍혔는지 콘솔에 찍힌다. 

package egovframework.example.welcomeWeb.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import egovframework.example.welcomeWeb.service.WelcomeWebService;
import egovframework.rte.psl.dataaccess.util.EgovMap;

@Controller
public class WelcomeWebController { 
	
	private final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private WelcomeWebService welcomeWebService;

	@RequestMapping(value = "/welcomeWeb.do")
	public String welcomeWeb(ModelMap model) {
		
		try {
			String nulls = null;
			
			System.out.println(nulls.toString());
			
			List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList(); 
			System.out.println(welcomeWebList);
			
			//List를 Map에 담아 출력해보기
			HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
			
			for (int i = 0; i < welcomeWebList.size(); i++) {
				exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
			}
			
			System.out.println("exceptionMap : " + exceptionMap);
			
			//담은 Map의 데이터를 배열로 출력해보기
			String[] arrStr = new String[exceptionMap.size()];
			
			for (int i = 0; i < exceptionMap.size()+2; i++) {
				arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
			}
			
			System.out.println("arrStr : " + Arrays.toString(arrStr));
			
			List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
			System.out.println(pieChartList);
			
			model.addAttribute("welcomeWebList", welcomeWebList);
			model.addAttribute("pieChartList", pieChartList);
		
		} catch (ArrayIndexOutOfBoundsException ae) {
			//System.out.println(ae + "배열의 길이를 확인해!");
			
			log.info(ae + "배열의 길이를 확인해!");
		} catch (NullPointerException ne) {
			//System.out.println(ne + "null있다.");
			
			log.info(ne + "null있다.");
		} catch (Exception e) {
			//System.out.println(e + "그 외 모든 오류들");
			
			log.debug(e + "그 외 모든 오류들");
		} finally {
			//System.out.println("welcomeWeb Controller에서 오류");
			//System.out.println("오류가 나던 안 나던 출력");
			log.debug("welcomeWeb Controller에서 오류");
			log.debug("오류가 나던 안 나던 출력");
		}
		
		return "welcomeWeb/welcomeWeb.tiles";
	}
}

 

 

++로그레벨 보는 법 
로그의 레벨을 설정한다고 한다. 왼쪽에서 오른쪽으로 읽는다. 

로그레벨

 

 

로그는 log4j2.xml에서 설정한다. 

egovframework를 사용하고 있기 때문에 여기에 설정한다.

 

 

test.log위치를 찾아서 log4j2.xml에 설정해준다.

 

프로그램을 실행시키면 콘솔창과 test.log에 잘 찍혀있는 것을 볼 수 있다.

 

syso하든 log를 찍든 그 목적은 파일을 떨구기 위해서다. 오류를 찾기보다 파일로 기록을 남기기 위해서다. 

책임을 떠맡기 싫어서 파일로 남겨둔다. 그래서 사용자가 로그인하거나 로그아웃할때 사용한다.

log는 데일리로 보관한다. 폴더명이 날짜별로 떨궈진다.

 

 

 

 

 

 

 

출처  :  한큐에 자바 수강내용 

포워딩(전송)을 하게되면 오류가 발생할 수 있는데 이 오류를 두 가지로 나눌 수 있다. 
컴파일오류와 실행오류이다. 
컴파일오류는 오타를 쳤을때고 Java에서 알려준다. 

실행오류는 예를 들어 무한 for문이다. 내가 생각한 결과물과 다른 결과물이 나올때다. 

서버는 잘뜨나 실행이 되지 않는다.

 

 

다루어볼 것은 실행오류에 대한 처리이다.
오류는 시스템 오류와 예외가 있다. 
시스템 오류는 잡을 수 없다.

예외는 내가 생각한 거와 다른 결과물로 프로그래밍으로 제어(제거)할 수가 있다.

 

모든 오류의 부모는 Throwable이다.
우리가 제어 할 수 있는 예외의 부모는 Exception이고, 
우리가 제어할 수 없는 시스템 오류의 부모는 error이다.


 



실무에서 사용하는 ExceptionHandling이다.

Exception오류는 콘솔에 안 뜬다. 왜냐하면 자바컴파일러는 오류로 생각하지 않기 때문이다. 

구조

실행오류예외처리 (RuntimeException)
try catch문을 사용해서 콘솔에 오류가 뜨게 만든다. 
try catch는 try구문 안에서 RuntimeException같은 종류의 오류가 발생하면 무조건 catch문을 타게된다. 


++실무에서 운영서버나 test서버에 프로젝트를 올리게 되면 Java파일을 올리는 것이 아니라 class파일을 올리게 된다. 
오류가 나게 되면 의지할 수 있는 곳은 콘솔 밖에 없다. 그러면 콘솔과 내 로컬 서버를 비교하면서 오류를 찾게 된다. 
그렇기 때문에 어떠한 오류가 났는지 확인 하도록 값을 같이 찍어준다.

 

@Controller
public class WelcomeWebController { 
	
	@Resource
	private WelcomeWebService welcomeWebService;

	@RequestMapping(value = "/welcomeWeb.do")
	public String welcomeWeb(ModelMap model) {
		
		try {
			//String nulls = null;
			
			//System.out.println(nulls.toString());
			
			List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList(); 
			System.out.println(welcomeWebList);
			
			//List를 Map에 담아 출력해보기
			HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
			
            //여기서 오류냄
			for (int i = 0; i < welcomeWebList.size()+2; i++) {
				exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
			}
			
			System.out.println("exceptionMap : " + exceptionMap);
			
			//담은 Map의 데이터를 배열로 출력해보기
			String[] arrStr = new String[exceptionMap.size()];
			
			for (int i = 0; i < exceptionMap.size(); i++) {
				arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
			}
			
			System.out.println("arrStr : " + Arrays.toString(arrStr));
			
			
			List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
			System.out.println(pieChartList);
			
			model.addAttribute("welcomeWebList", welcomeWebList);
			model.addAttribute("pieChartList", pieChartList);
		
		} catch (Exception e) {
			System.out.println(e + "그 외 모든 오류들");
		} 
		
		return "welcomeWeb/welcomeWeb.tiles";
	}
}

 

 


 



보통 array오류와 nullpoint오류가 많이 난다. 팀프로젝트에서는 test서버에서는 확인하기 힘들어서 꼭 예외 처리해준다.

일단, try문에 String 변수에다가 null을 넣어준다.

@Controller
public class WelcomeWebController { 
	
	@Resource
	private WelcomeWebService welcomeWebService;

	@RequestMapping(value = "/welcomeWeb.do")
	public String welcomeWeb(ModelMap model) {
		
		try {
			String nulls = null;
			
			System.out.println(nulls.toString());
			
			List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList(); 
			System.out.println(welcomeWebList);
			
			//List를 Map에 담아 출력해보기
			HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
			
			for (int i = 0; i < welcomeWebList.size(); i++) {
				exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
			}
			
			System.out.println("exceptionMap : " + exceptionMap);
			
			//담은 Map의 데이터를 배열로 출력해보기
			String[] arrStr = new String[exceptionMap.size()];
			
			for (int i = 0; i < exceptionMap.size()+2; i++) {
				arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
			}
			
			System.out.println("arrStr : " + Arrays.toString(arrStr));
			
			List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
			System.out.println(pieChartList);
			
			model.addAttribute("welcomeWebList", welcomeWebList);
			model.addAttribute("pieChartList", pieChartList);
		
		} catch (ArrayIndexOutOfBoundsException ae) {
			System.out.println(ae + "배열의 길이를 확인해!");
		} catch (NullPointerException ne) {
			System.out.println(ne + "null있다.");		
		
		return "welcomeWeb/welcomeWeb.tiles";
	}
}

변수를 참조해서 실행하는데 우리가 변수에 null을 했기 때문에 참조할 수 없다. 그래서 콘솔에 nullpoint오류가 난다.

 

 


 


여러 오류가 발생한다면, 앞에 오류가 나오면 그 뒤에 오류는 안나온다. 

그래서 무슨 오류가 나오는지 여러 오류처리를 해야한다.
어디 controller에서 나온 오류인지 확인할 수 있도록 controller명도 같이 찍어준다.

@Controller
public class WelcomeWebController { 
	
	@Resource
	private WelcomeWebService welcomeWebService;

	@RequestMapping(value = "/welcomeWeb.do")
	public String welcomeWeb(ModelMap model) throws Exception{
		
		try {
			String nulls = null;
			
			System.out.println(nulls.toString());
			
			List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList(); 
			System.out.println(welcomeWebList);
			
			//List를 Map에 담아 출력해보기
			HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
			
			for (int i = 0; i < welcomeWebList.size(); i++) {
				exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
			}
			
			System.out.println("exceptionMap : " + exceptionMap);
			
			//담은 Map의 데이터를 배열로 출력해보기
			String[] arrStr = new String[exceptionMap.size()];
			
			for (int i = 0; i < exceptionMap.size()+2; i++) {
				arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
			}
			
			System.out.println("arrStr : " + Arrays.toString(arrStr));
			
			List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
			System.out.println(pieChartList);
			
			model.addAttribute("welcomeWebList", welcomeWebList);
			model.addAttribute("pieChartList", pieChartList);
		
		} catch (ArrayIndexOutOfBoundsException ae) {
			System.out.println(ae + "배열의 길이를 확인해!");
			
			System.out.println("welcomeWeb Controller에서 오류");
		} catch (NullPointerException ne) {
			System.out.println(ne + "null있다.");
			
			System.out.println("welcomeWeb Controller에서 오류");
		} 
		
		return "welcomeWeb/welcomeWeb.tiles";
	}
}

 

 


 

 

오류가 나던 안 나던 무조건 타게 만드는 메서드는 finally이다. try catch문에서만 실행이 된다. 

@Controller
public class WelcomeWebController { 
	
	@Resource
	private WelcomeWebService welcomeWebService;

	@RequestMapping(value = "/welcomeWeb.do")
	public String welcomeWeb(ModelMap model) throws Exception{
		
		try {
			String nulls = null;
			
			System.out.println(nulls.toString());
			
			List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList(); 
			System.out.println(welcomeWebList);
			
			//List를 Map에 담아 출력해보기
			HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
			
			for (int i = 0; i < welcomeWebList.size(); i++) {
				exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
			}
			
			System.out.println("exceptionMap : " + exceptionMap);
			
			//담은 Map의 데이터를 배열로 출력해보기
			String[] arrStr = new String[exceptionMap.size()];
			
			for (int i = 0; i < exceptionMap.size()+2; i++) {
				arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
			}
			
			System.out.println("arrStr : " + Arrays.toString(arrStr));
			
			List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
			System.out.println(pieChartList);
			
			model.addAttribute("welcomeWebList", welcomeWebList);
			model.addAttribute("pieChartList", pieChartList);
		
		} catch (ArrayIndexOutOfBoundsException ae) {
			System.out.println(ae + "배열의 길이를 확인해!");
		} catch (NullPointerException ne) {
			System.out.println(ne + "null있다.");
		} finally {
			System.out.println("welcomeWeb Controller에서 오류");
			
			System.out.println("오류가 나던 안 나던 출력");
		}
		
		return "welcomeWeb/welcomeWeb.tiles";
	}
}

 

 


 


모든 실행오류를 적어줄 수 없지만 어떤 오류인지 알수 없기 때문에 최상위 Exception를 적어준다. 
이 최상위를 위에 적어주면 나머지 오류는 실행되지 않기 때문에 최상위 Exception을 맨 아래에 넣는다.

이 오류는 꼭 있어야 하는 처리문이다.

@Controller
public class WelcomeWebController { 
	
	@Resource
	private WelcomeWebService welcomeWebService;

	@RequestMapping(value = "/welcomeWeb.do")
	public String welcomeWeb(ModelMap model) throws Exception{
		
		try {
			String nulls = null;
			
			System.out.println(nulls.toString());
			
			List<EgovMap> welcomeWebList = welcomeWebService.selectWelcomeWebServiceList(); 
			System.out.println(welcomeWebList);
			
			//List를 Map에 담아 출력해보기
			HashMap<String, Object> exceptionMap = new HashMap<String, Object>();
			
			for (int i = 0; i < welcomeWebList.size(); i++) {
				exceptionMap.put((String) welcomeWebList.get(i).get("seqNo"), welcomeWebList.get(i).getValue(1));
			}
			
			System.out.println("exceptionMap : " + exceptionMap);
			
			//담은 Map의 데이터를 배열로 출력해보기
			String[] arrStr = new String[exceptionMap.size()];
			
			for (int i = 0; i < exceptionMap.size()+2; i++) {
				arrStr[i] = (String) exceptionMap.get(String.valueOf(i+1));
			}
			
			System.out.println("arrStr : " + Arrays.toString(arrStr));
			
			List<EgovMap> pieChartList = welcomeWebService.selectPieChartList();
			System.out.println(pieChartList);
			
			model.addAttribute("welcomeWebList", welcomeWebList);
			model.addAttribute("pieChartList", pieChartList);
		
		} catch (ArrayIndexOutOfBoundsException ae) {
			System.out.println(ae + "배열의 길이를 확인해!");
		} catch (NullPointerException ne) {
			System.out.println(ne + "null있다.");
		} catch (Exception e) {
			System.out.println(e + "그 외 모든 오류들");
		} finally {
			System.out.println("welcomeWeb Controller에서 오류");
			
			System.out.println("오류가 나던 안 나던 출력");
		}
		
		return "welcomeWeb/welcomeWeb.tiles";
	}
}

 

 


 


try catch는 내가 책임지겠다는 것이고 throws exception는 나를 호출한 놈한테 책임을 주는 것이다. 책임회피다.
try랑 throw를 같이 적어주면 안된다.  controller에는 throw exception을 적어주지 않는 것을 추천한다. 

throw Exception의 controller에 적어주면 Java 뒷단에 책임을 지게 한다.

 

만약에 try랑 throws 둘다 안 적어주면 service에 오류가 난다.  

throws는 남이 호출한 메서드에 책임을 떠 앉게 해주기 때문이다.

 

 

 

 

 

 

 

출처  :  한큐에 자바 수강내용

+ Recent posts