Code example for PreparedStatement

Methods: executeUpdate, setBoolean, setInt, setNull, setString

0
 
		try { 
			updateStmt = connection.prepareStatement(QUERY_UPDATE);
			if(entity.getPattern()!=null)
			{ 
				updateStmt.setString(++index, entity.getPattern());
			}else{ 
				updateStmt.setNull(++index, Types.VARCHAR);
			} 
 
			if(entity.getSearchin()!=null)
			{ 
				updateStmt.setString(++index, entity.getSearchin().toString());
			}else{ 
				updateStmt.setNull(++index, Types.VARCHAR);
			} 
			 
			updateStmt.setInt(++index, entity.getHvalue());
			 
			if(entity.getDescription()!=null)
			{ 
				updateStmt.setString(++index, entity.getDescription());
			}else{ 
				updateStmt.setNull(++index, Types.VARCHAR);
			} 
 
			updateStmt.setBoolean(++index, entity.isActive());
 
			updateStmt.setInt(++index, entity.getId());
			recordsUpdated = updateStmt.executeUpdate();
			// this should affect at most one record 
			if (recordsUpdated == 0){
				throw new NoSuchEntityException(); 
			}else if(recordsUpdated == 1) {
				success = true;