본문 바로가기

Java/jdbc

일괄 삭제 하기

 	public boolean deleteCustUsers(String ids[]) {
 		String sql = " DELETE FROM CUSTUSER "
 					+ " WHERE ID=? ";
 		Connection conn = null;
 		PreparedStatement psmt = null;
 		
 		int count[] = new int[ids.length];
 		
 		try {
			conn = getConnection();
			conn.setAutoCommit(false); //넣다가 잘못될수있기떄문에 꺼준다
			
			psmt = conn.prepareStatement(sql);
			
			for(int i = 0; i< ids.length; i++) {
				psmt.setString(1, ids[i]);
				psmt.addBatch();//누적 축적 
			}
			count = psmt.executeBatch();
			
			conn.commit();
		} catch (SQLException e) {
			//문제 발생 지역
			e.printStackTrace();
			
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			//auto commit 켜주기 
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			close(conn, psmt, null);
		}
 		
 		boolean isS = true;
 		
 		for (int i = 0; i < count.length; i++) {
			if(count[i] != -2) {	// -2 -> 정상종료
				isS = false;
				break;
			}
		}
 		
 		return isS;
 	}

'Java > jdbc' 카테고리의 다른 글

jdbc Select  (0) 2019.12.24
jdbc update(DBConnection, DBClose 활용)  (0) 2019.12.23
jdbc delete  (0) 2019.12.23
jdbc Insert  (0) 2019.12.23
jdbc eclipse기본 설정하기  (0) 2019.12.23